Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jenny_pollack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
logs
49 changes: 49 additions & 0 deletions jenny_pollack/gulpfile.js
Original file line number Diff line number Diff line change
@@ -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']);
1 change: 1 addition & 0 deletions jenny_pollack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var tcp = require(__dirname + '/lib/tcp_server');
47 changes: 47 additions & 0 deletions jenny_pollack/lib/tcp_server.js
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions jenny_pollack/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
31 changes: 31 additions & 0 deletions jenny_pollack/test/tcp_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});