Node.js Routes: Adding route handlers to an already instantiated http server Node.js Routes: Adding route handlers to an already instantiated http server express express

Node.js Routes: Adding route handlers to an already instantiated http server


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).