Search code examples
node.jsrestexpressserver

Is it a bad practice to handle client's web page / static files requests and REST API requests on the same express server?


I've got a web app (single page React app) and I have an express server hosted on cloud run that handles all of my users' requests.

But I have some /api routes which right now are being handled on the same server. They usually call an external API or update something in the database.

For example:

/api/send-contact      // SENDS CONTACT FORM EMAIL
/api/search            // HANDLES TEXT SEARCH AND RESPONDS
/api/update-viewcount  // UPDATES THE DB VIEW COUNT FOR A PAGE

Is it a bad practice to handle those /api calls on the same express server that I use to respond to user's page requests? Should I build a separate server just for those "API" calls?


Solution

  • Server architecture contains many tradeoffs. To get higher scale, you typically add complexity (server clustering, offloading static resources, dynamically spinning up more servers, etc...). But, you don't want to add unnecessary complexity as that may cost more and is more complicated to manage and monitor and maintain.

    So, offloading static resources to some other server (a CDN or an NGINX instance or something like that) is one path on the road to adding scale to your server. But, you would only go there when you have a specific server scalability motivation for doing so. If your server is operating at 30% capacity now, there's no reason to go there yet.

    Is it a bad practice to handle those /api calls on the same express server that I use to respond to user's page requests?

    No. Not if your server is able to handle your current load just fine.

    Should I build a separate server just for those "API" calls?

    No. When you need more server scale and you've finished doing simple server optimizations to improve your scale, then you would consider getting some things off your API server such as serving static resources.