-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (50 loc) · 1.94 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
var http = require('http'),
url = require('url'),
path = require('path'),
mime = require('mime'),
fs = require('fs');
module.exports = function (port, root, wordy) {
if (wordy === undefined) wordy = false;
if (port === undefined) port = 80;
var server = http.createServer(function (req, res) {
var reqUrl = req.url.replace(/(\.\.\/?)/g, '');
var uri = url.parse(reqUrl).pathname,
filename = path.join(root, uri);
if (uri.charAt(uri.length - 1) === '/') {
filename += 'index.html';
}
if (wordy) console.log('GET:' + uri + ' -> ' + filename);
fs.exists(filename, function (exists) {
if (!exists) {
if (wordy) console.log('Error 404');
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Error 404');
return;
}
if (fs.statSync(filename).isDirectory() && uri.charAt(uri.length - 1) !== '/') {
if (wordy) console.log('Redirection 303');
res.writeHead(303,{ 'Location': uri + '/' });
res.end('Redirecting to: ' + uri + '/');
return;
}
fs.readFile(filename, 'binary', function(err, file) {
if(err) {
if (wordy) console.log('Error 500');
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Error 500');
return;
}
if (wordy) console.log('Ok 200');
res.writeHead(200, {
'Content-Type': mime.lookup(filename) + ';' + mime.charsets.lookup(mime.lookup(filename))
});
res.write(file, 'binary');
res.end();
});
});
});
console.log('The server is now launch on: http://localhost:' + port);
server.listen(port);
return server;
}