forked from michaelliao/learn-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_server.js
More file actions
34 lines (27 loc) · 948 Bytes
/
Copy pathfile_server.js
File metadata and controls
34 lines (27 loc) · 948 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
'use strict';
// a simple http server
var
fs = require('fs'),
url = require('url'),
path = require('path'),
http = require('http');
var root = path.resolve(process.argv[2] || '.');
console.log('Static root dir: ' + root);
var server = http.createServer(function (request, response) {
var
pathname = url.parse(request.url).pathname, // '/static/bootstrap.css'
filepath = path.join(root, pathname); // '/srv/www/static/bootstrap.css'
fs.stat(filepath, function (err, stats) {
if (!err && stats.isFile()) {
console.log('200 ' + request.url);
response.writeHead(200);
fs.createReadStream(filepath).pipe(response);
} else {
console.log('404 ' + request.url);
response.writeHead(404);
response.end('404 Not Found');
}
});
});
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');