-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_server.js
More file actions
28 lines (25 loc) · 847 Bytes
/
file_server.js
File metadata and controls
28 lines (25 loc) · 847 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
const fs = require("fs"),
url = require("url"),
path = require("path"),
http = require("http");
let root = path.resolve(process.argv[2] || ".");
console.log(`static root dir ${root}`);
let server = http.createServer((request, response) => {
let pathname = url.parse(request.url).pathname;
let filepath = path.join(root, pathname);
console.log(`pathname is ${pathname}`);
console.log(`filepath is ${filepath}`);
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(4444);
console.log(`server is running at http://127.0.0.1:4444`);