-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile-server.js
More file actions
28 lines (25 loc) · 830 Bytes
/
file-server.js
File metadata and controls
28 lines (25 loc) · 830 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
var util = require("util"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");
var dir = process.argv[2] || './public';
var port = parseInt(process.argv[3]) || 8080;
util.log('[' + process.pid + '] Serving files from ' + dir + ', port is ' + port);
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), dir, uri);
path.exists(filename, function(exists) {
if(exists) {
fs.readFile(filename, function(err, data) {
response.writeHead(200);
response.end(data);
});
} else {
util.log('File not found: ' + filename);
response.writeHead(404);
response.end();
}
});
}).listen(port);
util.log("Server running at http://localhost:" + port);