I'm a beginner in Node.js and I have implemented an express app with both client module and server module as a part of single project. I start server by calling
node server.js
I'm using express.static to refer to the client code and getting index.html of client.
app.use(express.static(__dirname + "/client"));
Now, what if I don't want client and server to be a part of same project? How should the express.static statement be written? Client's project can be located in some other directory and "__dirname" would not work in that case. How should client and server be made independent of each other's directory location?
In production it is standard to expose your client app with an http server which is better in serving static files, e.g. nginx.
All you're doing is serving files with express as though its a standard HTTP server. Its popular to serve these files with something like Nginx instead of relying on express to serve the files. Nginx scales better for this kind of thing. Its also possible to use a CDN to distribute your content to get it closer to your end user.
Either way, using express isn't horrible, but if you plan to scale its probably easier to scale the backend independent from the frontend because the backend is going to be a lot more resource hungry than a process serving static files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With