如何在同一端口上运行不同的服务器?

huangapple go评论117阅读模式
英文:

How to run different server on same port?

问题

我已经创建了两个使用 Express 和 Node.js 的服务器,现在我想在同一个端口上运行这两个服务器,该端口是 localhost:8000,但它们有不同的端点。如何实现或者应该采取什么方法?

以下是参考的服务器代码:

服务器1:

const express = require("express");
const cors = require("cors");
const app = express();
const axios = require('axios');

app.use(cors());
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
const PORT = 8000;

app.get("/WeatherForcast", function (req, res) {
  axios.get('https://localhost:7173/WeatherForecast')
  .then(response => {
    res.status(200).json({ success: true, data: response.data});
  })
  .catch(error => {
    console.log(error);
  });
});

app.listen(PORT, function () {
  console.log(`Server is running on ${PORT}`);
});

服务器2:

const express = require("express");
const cors = require("cors");
const app = express();
const axios = require('axios');

app.use(cors());
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
const PORT = 8000;

app.get("/UserData", function (req, res) {
  axios.get('https://localhost:7173/UserData')
  .then(response => {
    res.status(200).json({ success: true, data: response.data});
  })
  .catch(error => {
    console.log(error);
  });
});

app.listen(PORT, function () {
  console.log(`Server is running on ${PORT}`);
});

目前当我运行它时,一个服务器运行正常,另一个服务器显示端口8000已被使用的错误。

英文:

I have created 2 server with express and node.js and now I want to run both the server on the same port which is localhost:8000 with different end points.
How it can be done or what can be the approach for it?

Attaching the server code for reference:-

Server1:-

const express = require("express");
const cors = require("cors");
const app = express();
const axios = require('axios');

app.use(cors());
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
const PORT = 8000;

app.get("/WeatherForcast", function (req, res) {
  axios.get('https://localhost:7173/WeatherForecast')
  .then(response => {
    res.status(200).json({ success: true, data: response.data});
  })
  .catch(error => {
    console.log(error);
  });
});

app.listen(PORT, function () {
  console.log(`Server is running on ${PORT}`);
});

Server2:-

const express = require("express");
const cors = require("cors");
const app = express();
const axios = require('axios');

app.use(cors());
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
const PORT = 8000;

app.get("/UserData", function (req, res) {
  axios.get('https://localhost:7173/UserData')
  .then(response => {
    res.status(200).json({ success: true, data: response.data});
  })
  .catch(error => {
    console.log(error);
  });
});

app.listen(PORT, function () {
  console.log(`Server is running on ${PORT}`);
});

Currently when I run it, one server runs and for other server an error is displayed that port 8000 is already in use.

答案1

得分: 1

你不能在同一端口上运行两个服务器。操作系统和TCP堆栈不允许这样做。

最简单的解决方案是在一个服务器上使用两个端点。

如果必须使用两个单独的服务器,那么你可以将它们分别运行在不同的端口上(这两个端口都不是公共端口),然后使用像nginx这样的工具来代理每个不同的路径到相应的服务器上。

所以,用户的请求首先发送到代理服务器,代理服务器检查请求的路径,然后根据请求的路径(在代理配置中设置)将其转发到你的两个服务器中的一个。

英文:

You can't run two servers on the same port. The OS and TCP stack won't allow it.

The easiest solution is to use two endpoints on one server.

If you have to have two separate servers, then you would run them both on separate ports (neither of which is the public port) and then use something like nginx to proxy each separate path to the appropriate server.

So, the user's request goes to the proxy and the proxy examines the path of the request and then forwards it to one of your two servers based on the path of the request (as setup in the proxy configuration).

答案2

得分: 0

不可能在同一端口上运行不同的服务器。

英文:

it is not possible to run different servers on same port

答案3

得分: 0

两个不同的服务器不能在同一个端口上托管,因为它会产生错误,例如“此端口当前正在使用”之类的错误。

可以做的是,不要创建多个服务器,而是创建一个单一的服务器,并定义不同的端点来管理代码流程。

英文:

Two different servers can not be hosted on same port as it will give the error i.e "this port is currently is use" something like this.

The thing that can be done is instead of creating multiple server you can create a single server and define different endpoints to manage the code flow.

huangapple
  • 本文由 发表于 2023年1月6日 16:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75028501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定