NodeJS Express - 两个端口上的独立路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28755281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:29:25  来源:igfitidea点击:

NodeJS Express - separate routes on two ports

node.jsexpressroutesports

提问by JKC

I have an express server, and while building it created several "helper" functions on their own routes. I'd like those routes to be accessed on a different port. Is there anyway to do this in express?

我有一个快速服务器,并且在构建它时在自己的路线上创建了几个“帮助”功能。我希望在不同的端口上访问这些路由。有没有办法在快递中做到这一点?

In the code below, the "/factory" route (and other functionality) would be on one port, and the helper routes of "/killallthings", "/listallthings", and "/killserver" would be on a separate port.

在下面的代码中,“/factory”路由(和其他功能)将在一个端口上,而“/killallthings”、“/listallthings”和“/killserver”的辅助路由将在一个单独的端口上。

Here is a simplified version of the code:

这是代码的简化版本:

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);
});

Is this possible with express?

这可以用快递吗?

回答by JKC

Based on Explosion Pills suggestion above, I modified the code in roughly this way:

根据上面的 Explosion Pills 建议,我大致这样修改了代码:

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);
});

I wish I knew how to give Explosion Pills the credit for the answer! :)

我希望我知道如何将答案归功于 Explosion Pills!:)

回答by Virendra Singh Rathore

If you are trying to create multiple servers then why not crate multiple bin/www files with different ports and configurations. Another way could be pass port number directly from command line.

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