-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyserve.js
87 lines (72 loc) · 2.81 KB
/
myserve.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// metalsmith plugin providing a simple http server for the static files, based on:
// https://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http
// https://gist.githubusercontent.com/amejiarosario/53afae82e18db30dadc9bc39035778e5/raw/21c6ccd9f2be7cdaaaa7d837d3d8271db4984d0f/static_server.js
// https://stackabuse.com/node-http-servers-for-static-file-serving/
// https://github.com/mayo/metalsmith-serve/blob/master/lib/index.js
// ***** This is not security-hardened nor robust for even small loads. IT IS NOT A PUBLIC SERVER.
// This is meant just for quick simple basic local preview of the static site.
'use strict';
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
var serve = function(port, rootDir) {
if (!port) port = 8080;
var f = function(files, metalsmith, nextstep) {
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
// file extension to MIME type
const mimes = {
'.css': 'text/css',
'.gif': 'image/gif',
'.html': 'text/html',
'.ico': 'image/x-icon',
'.jpg': 'image/jpeg',
'.js': 'text/javascript',
'.json': 'application/json',
'.pdf': 'application/pdf',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.txt': 'text/plain',
};
if (!rootDir) rootDir = metalsmith.destination();
if (!rootDir) rootDir = '.';
const resolvedBase = path.resolve(rootDir);
const safeSuffix = path.normalize(url.parse(req.url).path).replace(/^([\.\\])+/, '').replace(/([\/\\])+/,'/').toString();
var pathname = path.join(resolvedBase, safeSuffix);
var ext = path.parse(pathname).ext;
fs.stat(pathname, (err,stats) => {
if (err && err.code === 'ENOENT') {
console.log(" file not found");
res.statusCode = 404;
res.end('Not found');
return;
}
if (stats.isDirectory()) {
if (!pathname.endsWith('/')) pathname += '/';
pathname += 'index.html';
ext = '.html';
console.log(' found directory, trying ' + pathname);
}
fs.readFile(pathname, (err, data) => {
if (err && err.code === 'ENOENT') {
res.statusCode = 404;
res.end('Not found');
} else if (err) {
console.log(` error reading the file: ${err}`);
res.statusCode = 500;
res.end('Error');
} else {
res.setHeader('Content-type', mimes[ext] || 'text/plain');
res.end(data);
}
});
});
}).listen(parseInt(port));
console.log("Now serving at http://localhost:"+port);
console.log("Use ^C to quit");
nextstep();
};
return f;
};
module.exports = serve;