最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js Routes: Adding route handlers to an already instantiated http server - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Jun 9, 2014 at 12:36 Arjun Mehta asked Jun 4, 2014 at 16:23 Arjun MehtaArjun Mehta 2,5522 gold badges25 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8 +50

Well, an http server is nothing but an EventEmitter. It has a request event which is the one that handles the requests ing 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).

发布评论

评论列表(0)

  1. 暂无评论