diff --git a/servor.js b/servor.js index bc3b52b..b093f5c 100644 --- a/servor.js +++ b/servor.js @@ -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!`); @@ -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 @@ -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));