How would I add route handlers to an http server that already exists and has been instantiated?
All the routers I've found (including express) seem to require that they be passed into the http.createServer()
method.
For example with express:
var server = http.createServer(app);
My main criteria:
Example: passing server into SockJS
var http_server = http.createServer(); // agnostic
sockjs_server.installHandlers(http_server, options);
http_server.listen(...);
The way it's done in the sockjs source seems quite cryptic... but I think it involves traversing existing handlers and overwriting them with a custom router/handlers.
Thanks so much for any help!
Well, an http server is nothing but an EventEmitter
. It has a request
event which is the one that handles the requests coming from the clients.
So, one thing you can do is to make a wrapper function around the current handler function. For instance, let's suppose the existence of some express application:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('Hello World!');
});
var server = app.listen(8080);
So, now, you can simply go over the list of currently registered request
listeners in the server, remove the old listener functions and wrap them in a new one that handles your request the way you want. For instance, you can now create your own router and determine through which pipeline to send a request depending on its contents (i.e. path, content type, accepted languages, etc).
server.listeners('request').forEach(function(listener){
server.removeListener('request', listener);
server.on('request', function(req, res){
console.log('Before');
listener(req,res);
console.log('After');
});
});
In the example above you can see that I run a couple of console.log
statements around the actual execution of the listener function. In this case the listener function is actually the Express main request
handler. The express handler represents a pipeline, and by creating this new wrapper function you just added a new pipe at the beginning of the pipeline.
This technique would allow you to handle the request first and decide whether you want to send your own response and terminate the request here, or send it through a different pipeline or send the request down the express pipeline (i.e. the old listener function available to your new request handler closure).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With