NodeJS Express – 两个端口上的单独路由

我有一个快递服务器,并build立它在自己的路线上创build了几个“帮手”function。 我希望这些路由在不同的端口上访问。 反正有这样的expression?

在下面的代码中,“/ factory”路由(和其他function)将位于一个端口上,“/ killallthings”,“/ listallthings”和“/ killserver”的帮助路由将位于单独的端口上。

以下是代码的简化版本:

var express = require('express'); var things = []; var app = express(); var port = 8080; app.post('/factory/', function(req, res) { //Create a thing and add it to the thing array }); //Assume more functions to do to things here.... app.post('/killallthings/', function(req, res) { //Destroy all the things in the array }); app.post('/listallthings/', function(req, res) { // Return a list of all the things }); app.post('/killserver/', function(req,res){ //Kills the server after killing the things and doing clean up }); //Assume https options properly setup. var server = require('https').createServer(options, app); server.listen(port, function() { logger.writeLog('Listening on port ' + port); }); 

这可能与快递?

       

网上收集的解决方案 "NodeJS Express – 两个端口上的单独路由"

基于上面的爆炸药build议,我大致这样修改了代码:

 var express = require('express'); var things = []; var app = express(); var admin_app = express(); var port = 8080; var admin_port = 8081; app.post('/factory/', function(req, res) { //Create a thing and add it to the thing array }); //Assume more functions to do to things here.... admin_app.post('/killallthings/', function(req, res) { //Destroy all the things in the array }); admin_app.post('/listallthings/', function(req, res) { // Return a list of all the things }); admin_app.post('/killserver/', function(req,res){ //Kills the server after killing the things and doing clean up }); //Assume https options properly setup. var server = require('https').createServer(options, app); server.listen(port, function() { logger.writeLog('Listening on port ' + port); }); var admin_server = require('https').createServer(options, admin_app); admin_server.listen(admin_port, function() { logger.writeLog('Listening on admin port ' + admin_port); }); 

我希望我知道如何给爆炸药信贷的答案! 🙂

如果您尝试创build多个服务器,那么为什么不使用不同的端口和configuration来创build多个bin / www文件。 另一种方法可能是直接从命令行传递端口号。