File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## This week homework
2
+
3
+ Assignment:
4
+ Create a http server that listens to requests
5
+ http://localhost:8080/status -->
6
+ http://localhost:8080/add -->
7
+ http://localhost:8080/subtract -->
8
+
9
+ Reading
10
+ Callbacks
11
+ modules, require
12
+ http, http listen
13
+ refresh on command line
Original file line number Diff line number Diff line change
1
+ // To run this file: node index.js
2
+ // Then in your browser: http://localhost:8080
3
+ var http = require ( 'http' ) ;
4
+
5
+ var port = 8080 ;
6
+
7
+ var server = http . createServer ( ) ;
8
+
9
+ // Start the HTTP server, start listening for requests
10
+ server . listen ( port , function ( error ) {
11
+ if ( error ) {
12
+ console . log ( error ) ;
13
+ } else {
14
+ console . log ( 'api listening on port' , port ) ;
15
+ }
16
+ } ) ;
17
+
18
+ // Create a event handler for "request"
19
+ // this is an alternative way
20
+ server . on ( 'request' , function ( request , response ) {
21
+ console . log ( 'New http request received' , request . url ) ;
22
+ response . setHeader ( 'content-type' , 'text/html' ) ;
23
+ response . write ( '<html><head></head><body><h1>Hello world</h1></body></html>' ) ;
24
+ response . end ( ) ;
25
+ } ) ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " week1" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " Week 1 homework" ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "repository" : {
10
+ "type" : " git" ,
11
+ "url" : " git+https://github.com/HackYourFuture/Node.js.git"
12
+ },
13
+ "keywords" : [
14
+ " nodejs"
15
+ ],
16
+ "author" : " Erol" ,
17
+ "license" : " ISC" ,
18
+ "bugs" : {
19
+ "url" : " https://github.com/HackYourFuture/Node.js/issues"
20
+ },
21
+ "homepage" : " https://github.com/HackYourFuture/Node.js#readme"
22
+ }
You can’t perform that action at this time.
0 commit comments