Revelav3/app.js
CountBleck 18cca9b49e Read static files from the correct directory
By default, paths in Node.js are resolved based upon the current
directory. This is problematic when the server is started from the home
directory or through systemd. This commit ensures static files are read
relative to app.js's path, instead of the current directory.
2022-08-25 21:56:26 -07:00

40 lines
1 KiB
JavaScript

import createBareServer from '@tomphttp/bare-server-node';
import http from 'http';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import nodeStatic from 'node-static';
import * as custombare from './static/customBare.mjs';
const PORT = process.env.PORT || 3000;
const bareServer = createBareServer('/bare/', {
logErrors: false,
localAddress: undefined
});
const serve = new nodeStatic.Server(join(
dirname(fileURLToPath(import.meta.url)),
'static/'
));
const server = http.createServer();
server.on('request', (request, response) => {
if (custombare.route(request, response)) return true;
if (bareServer.shouldRoute(request)) {
bareServer.routeRequest(request, response);
} else {
serve.serve(request, response);
}
});
server.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen(PORT);
console.log(`Server running at http://localhost:${PORT}/.`);