后端和前端在同一个端口

Backend and Frontend on same port

我在 AWS 上有一个 ec2 Windows 实例,它在端口 80 上响应前端。我的后端在端口 5000 上 运行。有什么方法可以同时托管前端和后端在同一端口上,其余部分不使用客户端上的任何端口 API?

前端:

www.example.com

当前后端:

www.example.com:5000

我希望它是什么:

www.example.com/backend/

如何为后端和前端路由编写单个 index.js 或 server.js 文件?

避免必须指定端口号的唯一真正方法是对 HTTP 使用 post 80,或对 HTTPS 使用 443。

如果您是 运行 IIS,您可以将前端 运行 作为一个名为 "example.com" 的网站,然后在该网站下有另一个 "application" 名为 "backend".

www.example.com 的所有 HTTP 请求都将转到根网站。对 www.example.com/backend 的请求将路由到 example.com 网站下的 "backend" 应用程序。

不过,ServerFault 可能更适合询问 IIS 问题。

当然,将两者托管在同一个端口上是微不足道的,这只是路由问题。

例如,使用 express.js 并在名为 public:

的文件夹中包含静态文件(CSS、图像、HTML 等)
const express = require('express')
const app = express()

app.use('/', express.static('public'))
app.get('/backend', (req, res) => res.send('Hello World!'))

app.listen(80, () => console.log('Example app listening on port 80!'))

如果制作文件public/index.html:

<html>HI</html>

然后就可以通过运行curl 'localhost:80/':

得到("frontend")
$ curl 'localhost:80/'
<html>HI</html>
$

您还可以访问您的 'backend':

$ curl 'localhost:80/backend'
Hello World!
$

我建议您在 Subdomains

中分离您的服务端点

Service Endpoint

The endpoint is a connection point where HTML files or active server pages are exposed. Endpoints provide information needed to address a Web service endpoint. The endpoint provides a reference or specification that is used to define a group or family of message addressing properties and give end-to-end message characteristics, such as references for the source and destination of endpoints, and the identity of messages to allow for uniform addressing of "independent" messages. The endpoint can be a PC, PDA, or point-of-sale terminal Reference: Definition of service endpoint.

对于您的前端端点,推荐的子域是:

  • http://www.example.com
  • http://example.com 对于这种情况,您必须重定向到子域 www

对于您的后端端点,您可以使用任何您想要的,但推荐的后端子域是:

  • http://api.example.com(用的最多)
  • http://backend.example.com

因此,对于您的情况,建议是:

您可以使用 Nginx 之类的反向代理或从 NodeJs 中的请求对象获取子域来完成此操作。

Nginx is a web server which can also be used as a reverse proxy, load balancer, and HTTP cache. The software was created by Igor Sysoev and first publicly released in 2004. A company of the same name was founded in 2011 to provide support.

第一种方法

Using Nginx as HTTP load balancer

您可以配置 Nginx 来平衡对您服务器的请求,如下所示:

http {
    upstream backend {
        server localhost:5000;
    }

    upstream frontend {
        server localhost;
    }

    server {
        listen 80;

        server_name api.example.com;
        location / {
            proxy_pass http://backend;
        }
    }

    server {
        listen 80;

        server_name www.example.com example.com;
        location / {
            proxy_pass http://frontend;
        }
    }
}

第二种方法

使用 expressjs 从请求对象中获取子域。

req.subdomains

An array of subdomains in the domain name of the request.

文档:

// Host: "tobi.ferrets.example.com"
req.subdomains
// => ["ferrets", "tobi"]

在您的情况下,您可能的子域是:wwwapi

// Host: "www.example.com"
req.subdomains
// => ["www"]

// Host: "api.example.com"
req.subdomains
// => ["api"]

这就是您在 server.js

中处理请求的方式
var subDomain = req.subdomains[0];
 
if (subdomain === 'api') {
    processBackendRequest();
} else {
    processFrontendRequest();
}