forked from cs4241-18a/a1-gettingstarted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (35 loc) · 1004 Bytes
/
server.js
File metadata and controls
46 lines (35 loc) · 1004 Bytes
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
43
44
45
46
var http = require('http')
, fs = require('fs')
, url = require('url')
, port = 8080;
var server = http.createServer (function (req, res) {
var uri = url.parse(req.url)
switch( uri.pathname ) {
case '/':
sendFile(res, 'index.html', 'text/html')
break;
case '/index.html':
sendFile(res, 'index.html', 'text/html')
break;
case '/styles.css':
res.writeHead(200, {'Content-type' : 'text/css'});
var fileContents = fs.readFileSync('./styles.css', {encoding: 'utf8'});
res.write(fileContents);
res.end();
break;
case '/scripts.js':
sendFile(res, 'scripts.js', 'text/javascript');
break;
default:
res.end('404 not found')
}
})
server.listen(process.env.PORT || port);
console.log('listening on 8080')
// subroutines
function sendFile(res, filename, type) {
fs.readFile(filename, function(error, content) {
res.writeHead(200, {'Content-type': type});
res.end(content, 'utf-8')
})
}