Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions servor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = async ({

// Configure globals

root = root.startsWith('/') ? root : path.join(process.cwd(), root);
root = root.startsWith('/') || ~/^[a-z]:/i.test(root) ? root : path.join(process.cwd(), root);

if (!fs.existsSync(root)) {
console.log(`[ERR] Root directory ${root} does not exist!`);
Expand Down Expand Up @@ -116,9 +116,14 @@ module.exports = async ({
const uri = path.join(root, pathname);
let ext = uri.replace(/^.*[\.\/\\]/, '').toLowerCase();
if (!fs.existsSync(uri)) return sendError(res, 404);
fs.readFile(uri, 'binary', (err, file) =>
err ? sendError(res, 500) : sendFile(res, 200, file, ext)
);
fs.readFile(uri, 'binary', (err, file) => {
if (err) return sendError(res, 500)

if (pathname.endsWith(".html"))
file = file + inject + livereload

sendFile(res, 200, file, ext)
});
};

// Respond to requests without a file extension
Expand Down Expand Up @@ -152,11 +157,17 @@ module.exports = async ({
// Start the server and route requests

server((req, res) => {
const decodePathname = decodeURI(url.parse(req.url).pathname);
const pathname = path.normalize(decodePathname).replace(/^(\.\.(\/|\\|$))+/, '');
let pathname = decodeURI(url.parse(req.url).pathname);

res.setHeader('access-control-allow-origin', '*');
if (reload && pathname === '/livereload') return serveReload(res);
if (!isRouteRequest(pathname)) return serveStaticFile(res, pathname);
if (reload && pathname === '/livereload')
return serveReload(res);

pathname = path.normalize(pathname).replace(/^(\.\.(\/|\\|$))+/, '');

if (!isRouteRequest(pathname))
return serveStaticFile(res, pathname);

return serveRoute(res, pathname);
}).listen(parseInt(port, 10));

Expand Down