NodeJS Express - 两个端口上的独立路由 [英] NodeJS Express - separate routes on two ports

查看:15
本文介绍了NodeJS Express - 两个端口上的独立路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速服务器,在构建它的同时在自己的路由上创建了几个帮助"函数.我希望在不同的端口上访问这些路由.无论如何要在快递中做到这一点?

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?

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

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.

这是代码的简化版本:

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

这可以通过 express 实现吗?

Is this possible with express?

推荐答案

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

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

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

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

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

这篇关于NodeJS Express - 两个端口上的独立路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆