From e5ce0aced3439ccfbe8cda567c879a6c1237724d Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 09:14:58 -0800 Subject: [PATCH 01/12] first commit, basic tcp server --- matthew_ringel/tcp_server.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 matthew_ringel/tcp_server.js diff --git a/matthew_ringel/tcp_server.js b/matthew_ringel/tcp_server.js new file mode 100644 index 0000000..f85f4c6 --- /dev/null +++ b/matthew_ringel/tcp_server.js @@ -0,0 +1,17 @@ +var net = require('net'); +var fs = require('fs'); +var os = require('os'); + +var server = net.createServer(function(socket) { + socket.on('data', function(data) { + console.log(data.toString()); + }); + + socket.on('end', function() { + console.log('socket closed'); + }); +}); + +server.listen('3000', function() { + console.log('server running on port 3000'); +}); From 18ec39d51ff656fbe1b745cf30ac9dfb418fb0a7 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 09:37:46 -0800 Subject: [PATCH 02/12] saves file with unique name --- matthew_ringel/log/1446571185889 | 5 +++++ matthew_ringel/log/1446571597310 | 10 ++++++++++ matthew_ringel/tcp_server.js | 4 +++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 matthew_ringel/log/1446571185889 create mode 100644 matthew_ringel/log/1446571597310 diff --git a/matthew_ringel/log/1446571185889 b/matthew_ringel/log/1446571185889 new file mode 100644 index 0000000..d23ce5c --- /dev/null +++ b/matthew_ringel/log/1446571185889 @@ -0,0 +1,5 @@ +GET / HTTP/1.1 +Host: localhost:3000 +User-Agent: curl/7.43.0 +Accept: */* + diff --git a/matthew_ringel/log/1446571597310 b/matthew_ringel/log/1446571597310 new file mode 100644 index 0000000..66c0bb0 --- /dev/null +++ b/matthew_ringel/log/1446571597310 @@ -0,0 +1,10 @@ +POST / HTTP/1.1 +Host: localhost:3000 +Accept-Encoding: gzip, deflate +Cookie: +User-Agent: node-superagent/0.18.2 +Content-Type: application/json +Content-Length: 17 +Connection: close + +{"hello":"world"} \ No newline at end of file diff --git a/matthew_ringel/tcp_server.js b/matthew_ringel/tcp_server.js index f85f4c6..19dabb9 100644 --- a/matthew_ringel/tcp_server.js +++ b/matthew_ringel/tcp_server.js @@ -4,7 +4,9 @@ var os = require('os'); var server = net.createServer(function(socket) { socket.on('data', function(data) { - console.log(data.toString()); + var date = new Date(); + var filename = String(date.getTime()); + fs.writeFileSync(__dirname + "/log/" + filename,data.toString()); }); socket.on('end', function() { From ea3e31cf410f202fb59ff5f348073a5441bd4ef5 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 09:58:05 -0800 Subject: [PATCH 03/12] added package.json --- matthew_ringel/.gitignore | 1 + matthew_ringel/package.json | 26 ++++++++++++++++++++++++++ matthew_ringel/tcp_server.js | 1 + 3 files changed, 28 insertions(+) create mode 100644 matthew_ringel/.gitignore create mode 100644 matthew_ringel/package.json diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore new file mode 100644 index 0000000..cf70988 --- /dev/null +++ b/matthew_ringel/.gitignore @@ -0,0 +1 @@ +**/node_modules diff --git a/matthew_ringel/package.json b/matthew_ringel/package.json new file mode 100644 index 0000000..c0ef684 --- /dev/null +++ b/matthew_ringel/package.json @@ -0,0 +1,26 @@ +{ + "name": "simpletcp", + "version": "0.1.0", + "description": "simple tcp server that saves each REST request to a unique file", + "main": "tcp_server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mringel/create_a_tcp_server.git" + }, + "keywords": [ + "tcp", + "node" + ], + "author": "matthew ringel", + "license": "MIT", + "bugs": { + "url": "https://github.com/mringel/create_a_tcp_server/issues" + }, + "homepage": "https://github.com/mringel/create_a_tcp_server#readme" +} diff --git a/matthew_ringel/tcp_server.js b/matthew_ringel/tcp_server.js index 19dabb9..614114f 100644 --- a/matthew_ringel/tcp_server.js +++ b/matthew_ringel/tcp_server.js @@ -7,6 +7,7 @@ var server = net.createServer(function(socket) { var date = new Date(); var filename = String(date.getTime()); fs.writeFileSync(__dirname + "/log/" + filename,data.toString()); + socket.end(); }); socket.on('end', function() { From afccfe9c54bb1c656292de9b859cbcca657ce76a Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 10:09:36 -0800 Subject: [PATCH 04/12] working on testing --- matthew_ringel/test/tcp_test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 matthew_ringel/test/tcp_test.js diff --git a/matthew_ringel/test/tcp_test.js b/matthew_ringel/test/tcp_test.js new file mode 100644 index 0000000..dd92356 --- /dev/null +++ b/matthew_ringel/test/tcp_test.js @@ -0,0 +1,17 @@ +var chai = require('chai'); +var chaiHttp = require('chai-http'); + +chai.use(chaiHttp); + +var appServer = 'http://localhost:3000'; + +describe('an http request', function() { + it('should have a status of 200', function() { + chai.request(appServer) + .get('/') + .then(function (res) { + console.log(res); + expect(res).to.have.status(200); + }); + }); +}); From 6ddb661159347debae2c325f7a889e1a9969bc65 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 10:56:06 -0800 Subject: [PATCH 05/12] chai-http test confirms file contents --- matthew_ringel/test/tcp_test.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/matthew_ringel/test/tcp_test.js b/matthew_ringel/test/tcp_test.js index dd92356..910fa87 100644 --- a/matthew_ringel/test/tcp_test.js +++ b/matthew_ringel/test/tcp_test.js @@ -1,17 +1,33 @@ var chai = require('chai'); var chaiHttp = require('chai-http'); +var fs = require('fs'); chai.use(chaiHttp); var appServer = 'http://localhost:3000'; describe('an http request', function() { - it('should have a status of 200', function() { + // it('should have a status of 200', function() { + // chai.request(appServer) + // .get('/') + // .then(function (res) { + // console.log(res); + // expect(res).to.have.status(200); + // }); + // }); + + it('should log a file containing "chai-test: true"', function() { + var newFile; chai.request(appServer) - .get('/') - .then(function (res) { - console.log(res); - expect(res).to.have.status(200); + .put('/test') + .set('chai-test', 'true') + .then(function (done) { + fs.watch(__dirname + '/../log/', function(event, filename) { + newFile = filename; + done(); + }); + var file = fs.readFileSync(String(newFile)); + expect(file.toString()).to.contain('chai-test: true'); }); }); }); From 1975c7d922df447c0bc95db6d58dd4e7f1d73929 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 10:57:31 -0800 Subject: [PATCH 06/12] updated gitignore to include log --- matthew_ringel/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore index cf70988..b0547e5 100644 --- a/matthew_ringel/.gitignore +++ b/matthew_ringel/.gitignore @@ -1 +1,2 @@ **/node_modules +**/log/** From 44b98358bfc7872be08335b19de618f0e6326359 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 10:59:54 -0800 Subject: [PATCH 07/12] folder cleanup --- matthew_ringel/log/1446571185889 | 5 ----- matthew_ringel/log/1446571597310 | 10 ---------- 2 files changed, 15 deletions(-) delete mode 100644 matthew_ringel/log/1446571185889 delete mode 100644 matthew_ringel/log/1446571597310 diff --git a/matthew_ringel/log/1446571185889 b/matthew_ringel/log/1446571185889 deleted file mode 100644 index d23ce5c..0000000 --- a/matthew_ringel/log/1446571185889 +++ /dev/null @@ -1,5 +0,0 @@ -GET / HTTP/1.1 -Host: localhost:3000 -User-Agent: curl/7.43.0 -Accept: */* - diff --git a/matthew_ringel/log/1446571597310 b/matthew_ringel/log/1446571597310 deleted file mode 100644 index 66c0bb0..0000000 --- a/matthew_ringel/log/1446571597310 +++ /dev/null @@ -1,10 +0,0 @@ -POST / HTTP/1.1 -Host: localhost:3000 -Accept-Encoding: gzip, deflate -Cookie: -User-Agent: node-superagent/0.18.2 -Content-Type: application/json -Content-Length: 17 -Connection: close - -{"hello":"world"} \ No newline at end of file From 83f3373ec53c8af9070692a79fd86a177169f245 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 11:01:16 -0800 Subject: [PATCH 08/12] cleaned test file --- matthew_ringel/test/tcp_test.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/matthew_ringel/test/tcp_test.js b/matthew_ringel/test/tcp_test.js index 910fa87..bad3bb6 100644 --- a/matthew_ringel/test/tcp_test.js +++ b/matthew_ringel/test/tcp_test.js @@ -7,14 +7,6 @@ chai.use(chaiHttp); var appServer = 'http://localhost:3000'; describe('an http request', function() { - // it('should have a status of 200', function() { - // chai.request(appServer) - // .get('/') - // .then(function (res) { - // console.log(res); - // expect(res).to.have.status(200); - // }); - // }); it('should log a file containing "chai-test: true"', function() { var newFile; From 8fd1ed9532b7a0233383d52b67357fb889c6fc67 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 11:15:48 -0800 Subject: [PATCH 09/12] added submission README --- matthew_ringel/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 matthew_ringel/README.md diff --git a/matthew_ringel/README.md b/matthew_ringel/README.md new file mode 100644 index 0000000..366c2c8 --- /dev/null +++ b/matthew_ringel/README.md @@ -0,0 +1,11 @@ +# Simple TCP Server +**Matthew Ringel +Code Fellows sea-d45-javascript +November 3, 2015** + +This is a simple tcp server that takes any REST request and saves +it to a unique file in the log directory with a filename based +on the system time when saved. + +Mocha testing uses to chai-http to verify that the file saved has +the contents of the request. From 9fc1658cbea622f7067f71637ec3b4f318984c8f Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 11:21:09 -0800 Subject: [PATCH 10/12] fixed gitignore --- matthew_ringel/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore index b0547e5..6ebaadb 100644 --- a/matthew_ringel/.gitignore +++ b/matthew_ringel/.gitignore @@ -1,2 +1,2 @@ **/node_modules -**/log/** +/log/** From 46ebf3191276584cbe2934f1734c97ff2b2b1a6e Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 11:23:23 -0800 Subject: [PATCH 11/12] gitignore --- matthew_ringel/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore index 6ebaadb..cf70988 100644 --- a/matthew_ringel/.gitignore +++ b/matthew_ringel/.gitignore @@ -1,2 +1 @@ **/node_modules -/log/** From bc77ff834f10b8cf80eb1f9a8a9831cb827b6965 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 11:26:14 -0800 Subject: [PATCH 12/12] gitignore log --- matthew_ringel/.gitignore | 1 + matthew_ringel/log/.include | 0 2 files changed, 1 insertion(+) create mode 100644 matthew_ringel/log/.include diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore index cf70988..b8961eb 100644 --- a/matthew_ringel/.gitignore +++ b/matthew_ringel/.gitignore @@ -1 +1,2 @@ **/node_modules +/log/* diff --git a/matthew_ringel/log/.include b/matthew_ringel/log/.include new file mode 100644 index 0000000..e69de29