From 2061a6106c48f9d7433e2f62dc0d2f7510a57ee8 Mon Sep 17 00:00:00 2001 From: Iryna Bondarenko Date: Wed, 4 Nov 2015 14:28:15 -0800 Subject: [PATCH] Files added --- Iryna_Bondarenko/.gitignore | 2 ++ Iryna_Bondarenko/gulpfile.js | 29 +++++++++++++++++++++ Iryna_Bondarenko/package.json | 26 ++++++++++++++++++ Iryna_Bondarenko/public/index.html | 15 +++++++++++ Iryna_Bondarenko/server.js | 42 ++++++++++++++++++++++++++++++ Iryna_Bondarenko/test/test.js | 39 +++++++++++++++++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 Iryna_Bondarenko/.gitignore create mode 100644 Iryna_Bondarenko/gulpfile.js create mode 100644 Iryna_Bondarenko/package.json create mode 100644 Iryna_Bondarenko/public/index.html create mode 100644 Iryna_Bondarenko/server.js create mode 100644 Iryna_Bondarenko/test/test.js diff --git a/Iryna_Bondarenko/.gitignore b/Iryna_Bondarenko/.gitignore new file mode 100644 index 0000000..e212594 --- /dev/null +++ b/Iryna_Bondarenko/.gitignore @@ -0,0 +1,2 @@ + +node_modules diff --git a/Iryna_Bondarenko/gulpfile.js b/Iryna_Bondarenko/gulpfile.js new file mode 100644 index 0000000..b80df2b --- /dev/null +++ b/Iryna_Bondarenko/gulpfile.js @@ -0,0 +1,29 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var appFiles = ['index.js', '/*.js', 'bin/**/*.js']; +var testFiles = ['./Iryna_Bondarenko/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 + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint']); \ No newline at end of file diff --git a/Iryna_Bondarenko/package.json b/Iryna_Bondarenko/package.json new file mode 100644 index 0000000..fd0979b --- /dev/null +++ b/Iryna_Bondarenko/package.json @@ -0,0 +1,26 @@ +{ + "name": "http_server", + "version": "1.0.0", + "description": "creating http server with routes", + "main": "server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "keywords": [ + "http", + "server" + ], + "author": "irusiabondarenko@gmail.com", + "license": "ISC", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "gulp-jshint": "^1.12.0", + "mocha": "^2.3.3" + } +} diff --git a/Iryna_Bondarenko/public/index.html b/Iryna_Bondarenko/public/index.html new file mode 100644 index 0000000..7c7b8ec --- /dev/null +++ b/Iryna_Bondarenko/public/index.html @@ -0,0 +1,15 @@ + + + + + Greet website + + +

1 route: /time

+

This route says the local time in Seattle

+

2 route: /greet/name

+

Returns a greet string which takes the name parameter for greeting

+

3 route : /greet/name (Post request)

+

Returns JSON string {hello: 'some_name'}. I'm checking it via superagent in command line.

+ + \ No newline at end of file diff --git a/Iryna_Bondarenko/server.js b/Iryna_Bondarenko/server.js new file mode 100644 index 0000000..459cde3 --- /dev/null +++ b/Iryna_Bondarenko/server.js @@ -0,0 +1,42 @@ +var http = require('http'); +var fs = require('fs'); +var url = require('url') ; +var ReadStream = require('stream').Readable; + +var server = http.createServer(function(req, res) { + var resData= {}; + if (req.url === '/time' && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = "Local time in Seattle is " + time; + } + + var url = req.url; + var str_url = url.slice(0,7); + var name = url.slice(7); + + if (str_url === '/greet/' && req.method === 'GET') { + this.name = name; + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = "Hello " + this.name + "! Have a nice day!" + } + if (str_url === '/greet/' && req.method === 'POST') { + resData.status = 200; + resData.contentType = 'application/json'; + resData.data = JSON.stringify({hello: name}); + } + + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain' + }); + res.write(resData.data || 'not found'); + res.end(); +}); + +server.listen(3000, function() { + console.log('server up'); +}); + +var date = new Date (); +var time = date.toLocaleTimeString(); diff --git a/Iryna_Bondarenko/test/test.js b/Iryna_Bondarenko/test/test.js new file mode 100644 index 0000000..d3e5691 --- /dev/null +++ b/Iryna_Bondarenko/test/test.js @@ -0,0 +1,39 @@ +'use strict'; +var chai = require('chai'); +var expect = chai.expect; +var chaihttp = require('chai-http'); +chai.use(chaihttp); + +require(__dirname + '/../server'); + +describe('our server', function(){ + it('should get the time', function(done){ + chai.request('localhost:3000') + .get('/time') + .end(function(err, res){ + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + +it('should get the name as parameter and greet the name', function(done){ + chai.request('localhost:3000') + .get('/greet/name') + .end(function(err, res){ + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + +it("should post JSON string with some name", function(done){ + chai.request('localhost:3000') + .post('/greet/name') + .end(function(err, res){ + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); +}); \ No newline at end of file