简体   繁体   中英

Serve Static Files Using Express or Use Express To Only Access Backend?

For what reason would you serve static HTML/CSS/JS files via Express using routes, rather than just hitting these files directly and then using Express to purely interact with the backend?

For example, I could access a webpage directly ie www.test.com/login.html , which I have hosted somewhere, which in turn uses JS to make fetch requests to an Express server hosted somewhere.

Or I could solely point my domain to interact with the Express server via its IP address, and have Express serve up these static files.

For what reason would I use one over the other?

Hopefully this made sense, apologies if I've missed a trick - Relatively new to all this!

Thanks

For what reason would you serve static HTML/CSS/JS files via Express using routes?

If your files are truly static (no dynamic content in them at all), then it doesn't really matter who serves them. You can serve them from anywhere. You will have to then manage two domains and make sure your server is properly set up for CORS access. You can even use the same domain and use a proxy like NGINX that serves up just the static content while forwarding everything else through to your Express server.

If you are dynamically generating any content (like many web sites that use a template engine and generate HTML content from a database from the server), then it makes all the sense in the world to serve your content from the Express server.

I would not recommend interfacing with your Express server via direct IP address because that is far, far less flexible that using a DNS name when it comes to scaling, changing hosting facilities, load balancing, etc... So, if you're going to serve the static content from somewhere else, then either use a proxy on the same domain or use two separate domains.

The main reason to get static content off the Express server is when you want to increase scalability and your current load is too much for just the Express server. In that case, oftentimes the simplest way to get a bump in scalability is to turn the static file serving over to another system. If your website is still low scale, then I wouldn't suggest making things any more complicated than they need to be. Keep things simple at low scale. As your load increases, then invest in divesting of the static serving to another host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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