简体   繁体   English

同一端口的后端和前端

[英]Backend and Frontend on same port

I have a ec2 Windows instance on AWS, which responds with a frontend on port 80. My backend is running on port 5000. Is there any way I can host both frontend and backend on same port without using any port on a client for the rest API? 我在AWS上有一个ec2 Windows实例,它在端口80上有一个前端。我的后端在端口5000上运行。有没有办法我可以在同一端口上托管前端和后端,而不使用客户端上的任何端口API?

Frontend: 前端:

www.example.com

Current Backend: 当前后端:

www.example.com:5000

What I'd like it to be: 我希望它是什么:

www.example.com/backend/

How do I to write a single index.js or server.js file for both Backend and Frontend routes? 如何为后端和前端路由编写单个index.js或server.js文件?

I recommend you to separate your service Endpoints in Subdomains 我建议您将子域中的服务端点分开

Service Endpoint 服务端点

The endpoint is a connection point where HTML files or active server pages are exposed. 端点是公开HTML文件或活动服务器页面的连接点。 Endpoints provide information needed to address a Web service endpoint. 端点提供寻址Web服务端点所需的信息。 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 . 端点可以是PC,PDA或销售点终端参考: 服务端点的定义

For your Frontend endpoint the recommended subdomain is: 对于您的前端端点,建议的子域是:

  • http:// www .example.com http:// www .example.com
  • http://example.com For this case you have to redirect to subdomain www http://example.com 对于这种情况,您必须重定向到子域www

For your Backend endpoint you can use whatever you want, but the recommended subdomains for Backend are: 对于您的后端端点,您可以使用任何您想要的,但后端的推荐子域是:

  • http:// api .example.com (The most used) http:// api .example.com(最常用)
  • http:// backend .example.com http:// backend .example.com

So, in your case the recommendations are: 因此,在您的情况下,建议是:

You can accomplish that using either a Reverse Proxy like Nginx or getting the Subdomain from the request object in NodeJs. 您可以使用像Nginx这样的反向代理或从NodeJ中的请求对象获取子域来实现这一点。

Nginx is a web server which can also be used as a reverse proxy, load balancer, and HTTP cache. Nginx是一个Web服务器,也可以用作反向代理,负载均衡器和HTTP缓存。 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. 该软件由Igor Sysoev创建,于2004年首次公开发布。同名公司成立于2011年,以提供支持。

First approach 第一种方法

Using Nginx as HTTP load balancer 使用Nginx作为HTTP负载均衡器

You can configure Nginx to balance the requests to your server as follow: 您可以配置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;
        }
    }
}

Second approach 第二种方法

Use expressjs to get the subdomain from request object. 使用expressjs从请求对象中获取子域。

req.subdomains req.subdomains

An array of subdomains in the domain name of the request. 请求的域名中的一组子域。

Documentation: 文档:

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

In your case your possible subdomains are: www or api 在您的情况下,您可能的子域名是: wwwapi

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

Or 要么

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

This is how you have to process the request within your server.js 这是您在server.js处理请求的方式

var subDomain = req.subdomains[0];

if (subdomain === 'api') {
    processBackendRequest();
} else {
    processFrontendRequest();
}

The only real way to prevent having to specify a port number is use post 80 for HTTP, or 443 for HTTPS. 防止必须指定端口号的唯一真正方法是使用post 80 for HTTP,或443 for HTTPS。

If you're running IIS you can have your front end running as a website called "example.com", and then under that website have another "application" called "backend". 如果您正在运行IIS,您可以将您的前端作为名为“example.com”的网站运行,然后在该网站下有另一个名为“后端”的“应用程序”。

All HTTP requests for www.example.com would go to the root website. www.example.com的所有HTTP请求都将转到根网站。 Requests to www.example.com/backend would route to the "backend" application under the example.com website. 对www.example.com/backend的请求将路由到example.com网站下的“后端”应用程序。

ServerFault may be a more appropriate place to ask IIS questions however. 但是,ServerFault可能是一个更适合询问IIS问题的地方。

Sure, it's trivial to host both on the same port(s), it's just a question of routing. 当然,在同一个端口上托管这两个端口是微不足道的,这只是一个路由问题。

For example, using express.js and having static files (CSS, images, HTML, etc) in a folder named public : 例如,使用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!'))

If you make a file public/index.html : 如果您将文件public/index.html

<html>HI</html>

Then you can get it (the "frontend") by running curl 'localhost:80/' : 然后你可以通过运行curl 'localhost:80/'得到它(“前端”):

$ curl 'localhost:80/'
<html>HI</html>
$

You can also access your 'backend': 您还可以访问“后端”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM