Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cdea0a

Browse files
added express boilerplate
1 parent eb93e2c commit 9cdea0a

File tree

4 files changed

+25
-109
lines changed

4 files changed

+25
-109
lines changed

week3/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

week3/index.html

Lines changed: 0 additions & 75 deletions
This file was deleted.

week3/index.js

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
var fs = require('fs');
2-
var http = require('http');
2+
var express = require('express')
33

4-
var port = 8080;
5-
var server = http.createServer(handleRequest);
4+
var app = express();
65

7-
function handleRequest (req, res) {
8-
// console.log(req.url);
9-
switch (req.url) {
10-
case '/':
11-
fs.readFile(__dirname + '/index.html', 'utf-8', function (error, data) {
12-
if (error) {
13-
res.writeHead(500);
14-
res.write('Could not open file');
15-
} else {
16-
res.writeHead(200, {'Content-Type': 'text/html'});
17-
res.write(data);
18-
}
19-
res.end();
20-
});
21-
console.log('root');
22-
break;
23-
default :
24-
console.log("url: " + req.url + ", method: " + req.method);
25-
res.writeHead(200, {'Content-Type': 'application/json'});
26-
// res.write('{"1":"make tea","2":"do dishes","3":"go shopping"}');
27-
// res.write('{}');
28-
res.end();
29-
}
30-
}
6+
app.get('/todos', function(req, res) {
7+
res.send({label:'sometodo'})
8+
})
319

32-
server.listen(port, function (error) {
33-
if (error) {
34-
console.log(error);
35-
} else {
36-
console.log('listening on port 8080');
37-
}
10+
var port = 8080;
3811

39-
});
12+
app.listen(port, function(err) {
13+
if (err) return console.error(err)
14+
console.log('Listing to port', port)
15+
})

week3/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "week3",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"express": "^4.14.0"
13+
}
14+
}

0 commit comments

Comments
 (0)