From 599008bcfce1bd283a200c25e07b14c063a9a66c Mon Sep 17 00:00:00 2001 From: DAVID PARK Date: Tue, 10 Nov 2015 21:11:20 -0800 Subject: [PATCH 1/2] first commit. Work in progress --- david_park/.gitignore | 2 ++ david_park/hi.json | 1 + david_park/package.json | 17 +++++++++++++++++ david_park/server.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 david_park/.gitignore create mode 100644 david_park/hi.json create mode 100644 david_park/package.json create mode 100644 david_park/server.js diff --git a/david_park/.gitignore b/david_park/.gitignore new file mode 100644 index 0000000..902b281 --- /dev/null +++ b/david_park/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +data/ diff --git a/david_park/hi.json b/david_park/hi.json new file mode 100644 index 0000000..b1759f7 --- /dev/null +++ b/david_park/hi.json @@ -0,0 +1 @@ +utf8 \ No newline at end of file diff --git a/david_park/package.json b/david_park/package.json new file mode 100644 index 0000000..82f9453 --- /dev/null +++ b/david_park/package.json @@ -0,0 +1,17 @@ +{ + "name": "david_park", + "version": "1.0.0", + "description": "", + "main": "server.js", + "dependencies": { + "body-parser": "^1.14.1", + "express": "^4.13.3" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "author": "", + "license": "ISC" +} diff --git a/david_park/server.js b/david_park/server.js new file mode 100644 index 0000000..e048203 --- /dev/null +++ b/david_park/server.js @@ -0,0 +1,37 @@ +var express = require('express'); +var app = express(); +var http = require('http'); +var fs = require('fs'); +var bodyparser = require('body-parser'); + +//data coming in from a post request should be saved to a json file +//in "data" directory. Do not commit your data file to git. +// '/*' means any URL +app.post('/*', function(req, res) { + var filepath = req.url.slice(1) + '.json'; //slice removes char(0) + console.log(filepath); + fs.writeFile(filepath, 'utf8', function(err, data) { + if(err) return console.log('error thrown'); + console.log(data); + }); +}) + +//pipe ondata tostring +//take json data and directly pipe it (req.pipe) + +//A get request to the same url should return the data contained in +//the json file +app.get('/', function(req, res) { + res.send('Get request received!'); +}); + +/*app.get('/*', function(req, res) { + var route = req.url; + console.log(route); + fs.readFile('data' + route + '.json'); // +})*/ + +app.listen(3000, function () { + console.log('server up'); +}); + From d7e428cbd7cb2fc8d58c4f10d17e0b3688cfd635 Mon Sep 17 00:00:00 2001 From: DAVID PARK Date: Tue, 10 Nov 2015 21:41:40 -0800 Subject: [PATCH 2/2] second commit. Still work in progress --- david_park/package.json | 5 ++++- david_park/server.js | 17 +++++++++-------- david_park/test/app_test.js | 7 +++++++ 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 david_park/test/app_test.js diff --git a/david_park/package.json b/david_park/package.json index 82f9453..65dbec2 100644 --- a/david_park/package.json +++ b/david_park/package.json @@ -7,7 +7,10 @@ "body-parser": "^1.14.1", "express": "^4.13.3" }, - "devDependencies": {}, + "devDependencies": { + "chai": "^3.4.1", + "mocha": "^2.3.3" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js" diff --git a/david_park/server.js b/david_park/server.js index e048203..697b83f 100644 --- a/david_park/server.js +++ b/david_park/server.js @@ -8,28 +8,29 @@ var bodyparser = require('body-parser'); //in "data" directory. Do not commit your data file to git. // '/*' means any URL app.post('/*', function(req, res) { - var filepath = req.url.slice(1) + '.json'; //slice removes char(0) + var filepath = (__dirname + "'data' + req.url.slice(1) + '.json'"; //slice removes char(0) console.log(filepath); - fs.writeFile(filepath, 'utf8', function(err, data) { - if(err) return console.log('error thrown'); - console.log(data); + fs.writeFile(filepath, /*POSTDATA?*/, function(err, data) { //fs.writeFile(file, data[, options], callback) + if (err) return throw err; + console.log('it\'s saved!'); + + /*req.pipe().?*/ + }); }) //pipe ondata tostring //take json data and directly pipe it (req.pipe) -//A get request to the same url should return the data contained in -//the json file app.get('/', function(req, res) { res.send('Get request received!'); }); -/*app.get('/*', function(req, res) { +app.get('/*', function(req, res) { var route = req.url; console.log(route); fs.readFile('data' + route + '.json'); // -})*/ +}) app.listen(3000, function () { console.log('server up'); diff --git a/david_park/test/app_test.js b/david_park/test/app_test.js new file mode 100644 index 0000000..90da0e1 --- /dev/null +++ b/david_park/test/app_test.js @@ -0,0 +1,7 @@ +var chai = require('chai'); +var expect = chai.expect; +require(__dirname + '/../server'); + +describe('', function () { + +})