Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  1. Add routes to an existing server the way something like sockjs does it.
  2. Be agnostic to whatever router is already being used (if there is one)
  3. Not rely on an existing router "app" object to add the routes (creating a new one using a routing library would be fine).

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!

like image 487
Arjun Mehta Avatar asked Jun 04 '14 16:06

Arjun Mehta


1 Answers

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

like image 191
Edwin Dalorzo Avatar answered Sep 30 '22 10:09

Edwin Dalorzo