Node Server as middle layer

 · 2 mins read

Many big technology companies place Node.js web servers in front of real API’s backend at the moment, Why?

There are a few reasons:

  1. Performance Node.js is famous for non-blocking I/O and high concurrency, so it’s very suitable for purposes like forwarding or load-balance. These functionalities look very much like nginx.

  2. Reuse

    There are many different clients these days, including desktop, mobile web, phone apps, tablet apps, Android, iOS, Windows Phone, etc. If the clients called all the data backends directly, we’d have to implement the same assembly graph in each one. Instead, each client calls the same set of node.js endpoints and gets (more or less) the same JSON payload back.

  3. Security

    the node.js service can be a central place to check authentication, CSRF, and business logic (ie, is the user allowed to see/modify certain types of data). You obviously can’t trust the client to do these checks.

  4. Data decoration

    data backends typically serve up raw data that may need to be decorated for presentation. For example, i18n, number/date/currency formatting, and URLs can all be handled at the node.js layer and should not be in the data backend layer.

  5. Proxy

    This is a way to get around the Same Origin Policy of browsers. What’s more, we can put static resources on the server, which leads to that, if the request is referring to a static resource, node server could respond immediately; if it is a dynamic request, the server would forward it to the backend which has published serviece APIs.

    In addition, we can make data cache on the server. see more.

  6. SSR

    Server Side Rendering is drawing more and more attention from developers because people nowadays are making Single Page Application, but it has two obvious problems, one is homepage rendering speed, the other is seo(Search Engine Optimization).

    When web app is growing bigger and bigger, the first page normally is constituted of many contents and will launch massive requesting, so it sometimes would render very slow especially the network is bad. Thus, in some cases, people would go back to the ‘old’ way of rendering contents from server side…

    For the SEO, there is a project named prerender.io

    Search engines and social networks are always trying to crawl your pages, but they only see the javascript tags…

    We render your javascript in a browser, save the static HTML, and you return that to the crawlers!

    It is said that google has done very well in crwaling SPA. It can execute JS in a browser environment. BTW, it’ better to ensure using HTML5 history mode in your URL scheme SPA and SEO: Google (Googlebot) properly renders Single Page Application and execute Ajax calls

LINKS:

node 做中间层的作用和好处?

Why do some backend architectures place Node.js web servers in front of another API’s backend?