admin 管理员组文章数量: 1086019
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:
- Add routes to an existing server the way something like sockjs does it.
- Be agnostic to whatever router is already being used (if there is one)
- 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:
- Add routes to an existing server the way something like sockjs does it.
- Be agnostic to whatever router is already being used (if there is one)
- 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 badges1 Answer
Reset to default 8 +50Well, 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).
本文标签: javascriptNodejs Routes Adding route handlers to an already instantiated http serverStack Overflow
版权声明:本文标题:javascript - Node.js Routes: Adding route handlers to an already instantiated http server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744068440a2528045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论