-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (33 loc) · 963 Bytes
/
server.js
File metadata and controls
41 lines (33 loc) · 963 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
'use strict';
var http = require('http'),
fs = require('fs'),
path = require('path'),
url = require('url');
var mimeTypes = {
'html': 'text/html',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'png': 'image/png',
'js': 'text/javascript',
'css': 'text/css',
'jsx': 'text/jsx'
};
http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
if(exists && fs.lstatSync(filename).isDirectory()) {
exists = false;
}
if (!exists) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('404 Not Found\n');
res.end();
return;
}
var mimeType = mimeTypes[path.extname(filename).split('.')[1]];
res.writeHead(200, {'Content-Type': mimeType});
var fileStream = fs.createReadStream(filename);
fileStream.pipe(res);
});
}).listen(1337);