-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (46 loc) · 1.35 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var http = require('http')
var fs = require('fs')
var url = require('url');
var mime = require('mime');
function urlToPath(urlStr) {
var path = url.parse(urlStr).pathname;
return '.' + decodeURIComponent(path);
}
var methods = {};
methods.GET = function(path, respond) {
console.log("GET " + path);
fs.stat(path, function(error, stats) {
if (error && error.code == 'ENOENT') {
respond(404, 'File not found');
} else if (error) {
respond(500, 'Internal server error: ' + error.toString());
} else if (stats.isDirectory()) {
fs.readdir(path, function(error, files) {
if (error) {
respond(500, 'Internal server error: ' + error.toString());
} else {
respond(200, files.join('\n'));
}
});
} else {
respond(200, fs.createReadStream(path), mime.lookup(path));
}
})
};
var server = http.createServer(function(request, response) {
function respond(code, body, type) {
if (!type) type = 'text/plain';
response.writeHead(code, {'Content-Type': type});
if (body && body.pipe) {
body.pipe(response);
} else {
response.end(body);
}
}
if (request.method in methods) {
methods[request.method](urlToPath(request.url), respond, request);
} else {
respond(405, 'Method ' + request.method + ' not allowed');
}
});
server.listen(8000);