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 matthew_ringel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules
/log/*
11 changes: 11 additions & 0 deletions matthew_ringel/README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file added matthew_ringel/log/.include
Empty file.
26 changes: 26 additions & 0 deletions matthew_ringel/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
20 changes: 20 additions & 0 deletions matthew_ringel/tcp_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var net = require('net');
var fs = require('fs');
var os = require('os');

var server = net.createServer(function(socket) {
socket.on('data', function(data) {
var date = new Date();
var filename = String(date.getTime());
fs.writeFileSync(__dirname + "/log/" + filename,data.toString());
socket.end();
});

socket.on('end', function() {
console.log('socket closed');
});
});

server.listen('3000', function() {
console.log('server running on port 3000');
});
25 changes: 25 additions & 0 deletions matthew_ringel/test/tcp_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 log a file containing "chai-test: true"', function() {
var newFile;
chai.request(appServer)
.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');
});
});
});