简体   繁体   English

如何从 static 网页链接到在 80 以外的端口上侦听的 node.js 应用程序?

[英]How can I link to a node.js application listening on a port other than 80 from a static webpage?

I'm learning about web development and I'm wondering how to integrate some different technologies together.我正在学习 web 开发,我想知道如何将一些不同的技术集成在一起。 Assume I've got a static webpage hosted on a server with Apache.假设我有一个 static 网页托管在具有 Apache 的服务器上。 I've also got a node.js application running on that server, listening on port 9091. I want to be able to have a link on the static page that sends a get request to some route on the node.js app, and displays the html that's returned. I've also got a node.js application running on that server, listening on port 9091. I want to be able to have a link on the static page that sends a get request to some route on the node.js app, and displays the html that's returned . Of course, Apache is listening on port 80.当然,Apache 正在监听 80 端口。

Is what I'm describing possible?我描述的可能吗? Is it the best way to do what I'm trying to do?这是做我想做的最好的方法吗? I'm aware that I can do away with Apache entirely and just have the node.js application serve the static pages, but I've been told that's a bad practice because node.js is so much less efficient at hosting static pages. I'm aware that I can do away with Apache entirely and just have the node.js application serve the static pages, but I've been told that's a bad practice because node.js is so much less efficient at hosting static pages.

Thanks谢谢

You can link to a URL to a specific port number on a server by just putting the port number in the URL:您只需将端口号放入 URL 即可将 URL 链接到服务器上的特定端口号:

http://someserver.com:9091/somepath http://someserver.com:9091/somepath

The browser will contact that host on that port and send it the http request.浏览器将在该端口上联系该主机并向其发送 http 请求。


However, you probably don't want to do that because that will look messy on your site if the static pages show one port number in the URL and the dynamic pages show a different port number and it could really complicate maintenance of your site.但是,您可能不想这样做,因为如果 static 页面在 URL 中显示一个端口号,而动态页面显示不同的端口号,这会使您的站点看起来很混乱,这可能会使您的站点维护变得复杂。

First off, you should probably not go to a separate server for serving static pages until you've actually proven you have a need for that.首先,您可能不应该将 go 连接到单独的服务器以提供 static 页面,直到您实际证明您需要它。 Express can certainly be beaten by Apache of NGINX for serving static resources, but it's not horrible at it so unless you've maxed out your Express server, I wouldn't recommend adding complication until you've proven you need it. Express 当然可以被 NGINX 的 Apache 击败,因为它为 static 资源提供服务,但它并不可怕,所以除非你已经证明你需要它,除非你已经最大化你的 Express 服务器。 You can always add that extra scale later without making a big architectural change.您可以随时添加额外的规模,而无需进行大的架构更改。

Second off, a better way to handle static resources is with a proxy you put in front of your web server (NGINX is very often what is used with node.js).其次,处理 static 资源的更好方法是使用您放在 web 服务器前面的代理(NGINX 经常与 node.js 一起使用)。 Then, you put your proxy on port 80. It examines the URL and if it's a pattern in the URL that indicates a static resource, then it takes the request and serves the static resource. Then, you put your proxy on port 80. It examines the URL and if it's a pattern in the URL that indicates a static resource, then it takes the request and serves the static resource. If not, it forwards the request to your Express server which handles it.如果没有,它会将请求转发到处理它的 Express 服务器。 The Express server will be running on either a different host or (if no the same host as the proxy), then on a different port. Express 服务器将在不同的主机上运行,或者(如果与代理没有相同的主机),然后在不同的端口上运行。 But, the Express server's port is not visible to the outside world, only the proxy and it's port 80 is visible to the outside world.但是,Express 服务器的端口对外界是不可见的,只有代理和它的端口 80 对外界可见。 Everything goes to/from port 80 on the proxy.一切都进出代理上的端口 80。 Nice and simple.很好很简单。

The one thing you should do from the beginning is to make sure that there's an identifiable pattern in the URL that indicates whether the resource is static or dynamic.从一开始就应该做的一件事是确保 URL 中有一个可识别的模式,指示资源是 static 还是动态的。 This would be true whether using NGINX as a proxy or using express.static() to serve your static files from Express.无论是使用 NGINX 作为代理还是使用express.static()从 Express 提供 static 文件,这都是正确的。 One common trick is to use one or more path prefixes for all static resources such as /img , /css , /font , /script , etc... or put everything behind one prefix such as /static Then, it's easy to configure a future proxy for exactly which URLs it should grab for static resources and which it should forward on to the Express server.一个常见的技巧是为所有 static 资源(例如/img/css/font/script等)使用一个或多个路径前缀,或者将所有内容放在一个前缀之后,例如/static然后,很容易配置一个未来代理它应该为 static 资源获取哪些 URL,以及它应该转发到 Express 服务器。

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

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