-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhttp_server.js
More file actions
42 lines (35 loc) · 1.16 KB
/
http_server.js
File metadata and controls
42 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var http = require('http');
var fs = require('fs');
var ReadStream = require('stream').Readable;
var server = http.createServer(function(req, res){
var resData = {};
var name = req.url.slice(7);
if (req.url === '/' && req.method === 'GET'){
resData.status = 200;
resData.contentType = 'text/html';
resData.data = fs.readFileSync(__dirname + '/public/index.html').toString();
}
if(req.url === '/awesome' && req.method === 'GET'){
resData.status = 200;
resData.contentType = 'application/json';
resData.data = JSON.stringify({hello: 'world'});
}
if(req.url === '/greet/' + name && req.method === 'GET'){
resData.status = 200;
resData.contentType = 'text/plain'
resData.data = ('Greetings, ' + name + '.');
}
if(req.url === '/time' && req.method === 'GET'){
resData.status = 200;
resData.contentType = 'text/plain';
resData.data = ('The date/time of the previous GET request: ' + Date());
}
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');
});