Revelav3/app.js
CountBleck 4ff19c9fa5 Improve error handling in some cases
In Cyclone's fetchBare(), the content type wasn't correct, and the
status line should be "Internal Server Error", not just "Error". In
the main request handler, I added similar error handling, just in case.
2022-08-30 15:24:48 -07:00

47 lines
1.2 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) => {
try {
if (custombare.route(request, response)) return true;
if (bareServer.shouldRoute(request)) {
bareServer.routeRequest(request, response);
} else {
serve.serve(request, response);
}
} catch (e) {
response.writeHead(500, "Internal Server Error", {
"Content-Type": "text/plain"
})
response.end(e.stack)
}
});
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}/.`);