Reputation: 250
I'm learning about web development and I'm wondering how to integrate some different technologies together. Assume I've got a static webpage hosted on a server with 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. Of course, Apache is listening on port 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.
Thanks
Upvotes: 0
Views: 158
Reputation: 707308
You can link to a URL to a specific port number on a server by just putting the port number in the URL:
http://someserver.com:9091/somepath
The browser will contact that host on that port and send it the http request.
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.
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. 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. 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). 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. 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. 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. Everything goes to/from port 80 on the proxy. 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. This would be true whether using NGINX as a proxy or using express.static()
to serve your static files from Express. 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.
Upvotes: 2