-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
36 lines (30 loc) · 891 Bytes
/
server.js
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
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')
break
case '/index.html':
sendFile(res, 'index.html')
break
case '/swplanets.csv':
sendFile(res, 'swplanets.csv', 'text/csv');
break;
default:
res.end('404 not found')
}
})
server.listen(process.env.PORT || port);
console.log('listening on 8080')
// subroutines
function sendFile(res, filename, contentType) {
contentType = contentType || 'text/html';
fs.readFile(filename, function(error, content) {
res.writeHead(200, {'Content-type': contentType})
res.end(content, 'utf-8')
})
}