-
Notifications
You must be signed in to change notification settings - Fork 20
HTTP server assignment #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
|
|
||
| node_modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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']); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Greet website</title> | ||
| </head> | ||
| <body> | ||
| <h2>1 route: /time</h2> | ||
| <h3> This route says the local time in Seattle</h3> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make these |
||
| <h2>2 route: /greet/name </h2> | ||
| <h3> Returns a greet string which takes the name parameter for greeting <h3> | ||
| <h2> 3 route : /greet/name (Post request) </h2> | ||
| <h3> Returns JSON string {hello: 'some_name'}. I'm checking it via superagent in command line. </h3> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. declare all your variables together at the top of your function or file |
||
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beautiful styling on this code! :) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are tabbed in too far - they just need to be indented once from the |
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see previous comment |
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see previous comment |
||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are tabbed out a bit too far