Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Stack - Web Server and API Server: together or separate? [closed]

We are working on a large scaled web app that built on top of javascript frameworks and libraries (node, express/sails, angular, mongo etc).

We need to have a backend that from one hand serve the web application but on the other accept API calls for data (from the same app but from other origins as well) - very common these days.

One of the decision we want to take is should we separate the backend for Web Application server and API server or put them together.

For example should we have an express running serving both or should express serve the web app, static content, auth etc and have a separate Restify server serving the data.

What are the pros and cons for each agenda?

like image 390
Ben Diamant Avatar asked Nov 25 '25 18:11

Ben Diamant


1 Answers

A clean separation of concerns is always the way to go. If, as you said, your API-Server takes calls also from other Apps, I would suggest you separate the delivery of static files and your API. It leaves your more flexible in changing the way one or the other works. Another benefit is that your API only needs to worry about API Calls and not the delivery, which should make it answer even faster.

I'm gonna take this one even further and say: Use nginx to deliver your static Web-App files (if you are not working with server side templating). See also this Thread - nginx is faster with delivering static sources.

In my company, this is how we do it for every App and it turned out to work just great.

So the Pros:

  • Better performance for both - static delivery and API
  • Clean separation of concerns
  • More flexibility in changing one or the other

The only con is that you need to install and maintain two programs. But given that NodeJS is super easy to set up, that shouldn't be a showstopper.

EDIT As state by mnemosyn in the comments, if you separate your apps, you should still pull every request through the nginx Server to avoid some same-origin-policie issues. Within your nginx, you just have to configure a virtual-host pointing to your NodeJS-App and then proxying all request to a specified path (for example /api/) to that VHost. You can read about it here.

like image 102
David Losert Avatar answered Nov 27 '25 08:11

David Losert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!