在快递应用中为不同的端口使用不同的路由

快递/节点应用中是否可以将不同的路由configuration到不同的端口? 例如:'/ foo / bar'只能访问localhost:3000'/ bar / foo'只能访问localhost:3002

是的,但是您只需创build两台服务器,每台服务器都在自己的端口上,然后为每台服务器创build一个快速app对象,并在适当的应用程序对象上注册所需服务器的路由。 给定的服务器只侦听一个端口。

 const express = require('express'); // first server const app3000 = express(); app3000.get('/bar/foo', function(req, res) { // code here for port 3000 handler }); app3000.listen(3000); // second server const app3002 = express(); app3002.get('/foo/bar', function(req, res) { // code here for port 3002 handler }); app3002.listen(3002);