diff --git a/jenny_pollack/.gitignore b/jenny_pollack/.gitignore new file mode 100644 index 0000000..4363a92 --- /dev/null +++ b/jenny_pollack/.gitignore @@ -0,0 +1,2 @@ +node_modules +logs \ No newline at end of file diff --git a/jenny_pollack/gulpfile.js b/jenny_pollack/gulpfile.js new file mode 100644 index 0000000..fc11fd9 --- /dev/null +++ b/jenny_pollack/gulpfile.js @@ -0,0 +1,49 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['index.js', 'lib/**/*.js', 'bin/**/*.js']; +var testFiles = ['test/**/*.js']; + +gulp.task('jshint:test', function() { + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('jshint:app', function() { + return gulp.src(appFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('mocha', function(){ + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true + } + })) + .pipe(mocha({reporter: 'spec'})); +}); + + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint', 'mocha']); \ No newline at end of file diff --git a/jenny_pollack/index.js b/jenny_pollack/index.js new file mode 100644 index 0000000..7c540fb --- /dev/null +++ b/jenny_pollack/index.js @@ -0,0 +1 @@ +var tcp = require(__dirname + '/lib/tcp_server'); diff --git a/jenny_pollack/lib/tcp_server.js b/jenny_pollack/lib/tcp_server.js new file mode 100644 index 0000000..04926d4 --- /dev/null +++ b/jenny_pollack/lib/tcp_server.js @@ -0,0 +1,47 @@ +var net = require('net'); +var fs = require('fs'); + +var server = module.exports = net.createServer(function(socket){ + + var unixTimestamp = Date.now(); + var file = fs.createWriteStream(__dirname + '/../logs/' + unixTimestamp + '.txt'); + socket.pipe(file); + + socket.on('end', function(){ + console.log('connection ending'); + server.close(); + }); + +}); + +server.listen('3000', function(){ + console.log('server running'); +}); + +//use this format to pipe the socket +//to the file +//everything on unix is a file +//including the console +//so stdout is a file descriptor +//we can write to it in the exact +//same way we write to a file + +//fs.createWriteStream(path, [options]) +//the path should be afolder in your +//local directory +//timestamps is a good idea +//global unique identifier +//crypto.random to generate a random text string +// +// +//close the connection after you've +//logged the data + +//Write a simple tcp logging server. +//This server should receive tcp requests +//and save the request into a file. Each +//request should be saved into it's own file +//and you'll have to find something unique +//to name them. You can use a UUID library +//or the current time or any other means of +//having unique strings that you can think of. \ No newline at end of file diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json new file mode 100644 index 0000000..5cb645b --- /dev/null +++ b/jenny_pollack/package.json @@ -0,0 +1,27 @@ +{ + "name": "create_a_tcp_server", + "version": "0.1.0", + "description": "simple tcp server", + "main": "index.js", + "scripts": { + "test": "./node_modules/mocha/bin/mocha test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/jennypollack/create_a_tcp_server.git" + }, + "author": "Jenny Pollack", + "license": "MIT", + "bugs": { + "url": "https://github.com/jennypollack/create_a_tcp_server/issues" + }, + "homepage": "https://github.com/jennypollack/create_a_tcp_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3", + "jshint": "^2.8.0", + "mocha": "^2.3.3", + "superagent": "^1.4.0" + } +} diff --git a/jenny_pollack/test/tcp_test.js b/jenny_pollack/test/tcp_test.js new file mode 100644 index 0000000..c9ad136 --- /dev/null +++ b/jenny_pollack/test/tcp_test.js @@ -0,0 +1,31 @@ +var expect = require('chai').expect; +var fs = require('fs'); +var tcp = require(__dirname + '/../lib/tcp_server'); +var request = require('superagent'); +var net = require('net'); + +var requestsFilesBefore; +var requestsFilesAfter; + +describe('a request to the tcp server', function() { + + before(function(done) { + requestsFilesBefore = fs.readdirSync(__dirname + '/../logs/').length; + var client = net.createConnection({port: 3000}); + client.on('connect', function() { + client.write('test', function(){ + client.end(); + }); + }); + client.on('end', function() { + done(); + }); + }); + + it('should have written the file after the request', function() { + requestsFilesAfter = fs.readdirSync(__dirname + '/../logs/').length; + expect(requestsFilesBefore).to.eql(requestsFilesAfter - 1); + }); +}); + +