-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservestatic.js
47 lines (43 loc) · 1.23 KB
/
servestatic.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
const url = require("url");
const fs = require("fs");
const path = require("path");
const mimeType = {
".ico": "image/x-icon",
".html": "text/html",
".js": "text/javascript",
".json": "application/json",
".css": "text/css",
".png": "image/png",
".jpg": "image/jpeg",
".wav": "audio/wav",
".mp3": "audio/mpeg",
".svg": "image/svg+xml",
".pdf": "application/pdf",
".zip": "application/zip",
".doc": "application/msword",
".eot": "application/vnd.ms-fontobject",
".ttf": "application/x-font-ttf",
};
// +& MIDDLEWARE FOR ADDING CLASSIC RESPONSE FUNCIONS
// ! Borrowed from https://stackabuse.com/node-http-servers-for-static-file-serving/
const serveStatic = function (req, res) {
var resolvedBase = path.resolve(req.publicFolder);
var safeSuffix = path.normalize(req.url).replace(/^(\.\.[\/\\])+/, "");
var fileLoc = path.join(resolvedBase, safeSuffix);
fs.readFile(fileLoc, function (err, data) {
if (data) {
res.statusCode = 200;
const ext = path.parse(fileLoc).ext;
console.log(ext);
res.write(data);
res.setHeader("Content-type", mimeType[ext] || "text/plain");
}
if (err) {
res.json({ err });
}
// return res.end();
});
};
module.exports = {
serveStatic,
};