I’m building a NodeJS game engine. When the user creates an instance of the engine they must pass it an existing http server. The engine then attaches a socket server for updating the client side views.
What I’d like to do is serve a single JavaScript file at a special url that bridges the client side and allows the engine to setup its own sockets and provide the web page with a viewport and control binding api.
Is there anyway to add a request handler to an existing http server that can serve a single javascript based on the url?
I’ve tried this
function bindCoreFileServe(httpServer) {
//create a server
httpServer.on('request', function(req, res) {
//check the url
if(req.url === '/SliverJet.js') {
//send the datafunction (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
});
}
But it crashes node because the existing server also tries to send its own headers.
Thanks.
I figured this out via the source of socket.io. Essentially I detach all of the event listeners from the httpServer and store them in an array then I bind my own listener to it. If the url doesn’t belong to me I execute all of the detached event listeners with their call method passing it the httpServer as the scope.