Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating Express.js server and client projects

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?

like image 255
Developer Avatar asked Oct 31 '25 23:10

Developer


2 Answers

In production it is standard to expose your client app with an http server which is better in serving static files, e.g. nginx.

like image 105
alsdkjasdlkja Avatar answered Nov 04 '25 05:11

alsdkjasdlkja


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.

like image 34
tsturzl Avatar answered Nov 04 '25 07:11

tsturzl