This article is my collection of middleware and personal understanding of the record. It only represents personal level and personal understanding. If you have any questions, please be polite.

Wikipedia:

Middleware (English: Middleware) is the software that provides the connection between system software and application software, so as to facilitate the communication between software components, especially the centralized logic of application software to system software. It is widely used in modern information technology application frameworks such as Web services and service-oriented architecture. For example, database, Tomcat of Apache, WebSphere of IBM, WebLogic application server of BEA, Tong series middleware of Dongfang Tong and Kingdee company all belong to middleware.

Well, for a front-end scumbag, I don’t get it. Take a look at Baidu Encyclopedia. Here are some related middleware:

  • ESB (Enterprise Service Bus) : An ESB is an open, standards-based distributed synchronous or asynchronous messaging middleware. The ESB provides secure interoperability for enterprise applications through support for XML, Web Service interfaces, and standardized rules-based routing documents.
  • TP: Transaction Processing monitor: Provides monitoring capabilities for transactions that occur between objects to ensure that operations are successful.
  • Distributed Computing Environment (DCE) : A set of technical services required to create Distributed applications that run on different platforms.
  • Remote Procedure Call (RPC) : The standard required when a client sends a request to a server to run a program.
  • ORB: Object Request Broker: Provides an interface for users to communicate with objects in other distributed network environments.
  • Database Access Middleware: Enables users to Access databases in various operating systems or applications. SQL is one of these types of middleware.
  • Message Passing: E-mail systems are one of these middleware types.
  • Xml-based Middleware: XML allows developers to create documents for exchanging structured information over the Internet.

OK, here we go, still confused… Don’t be discouraged, middleware is a broad concept, so let’s narrow it down a bit and take a look at web-related middleware. It’s simple:

  • Components that decouple the concrete business from the underlying logic.
  • Data transfer station from the bottom to the application side.

The Node middleware

It seems that there is no real content, but some abstract concepts. Let’s get some practical knowledge about Node middleware. In recent years, microservices architecture has been promoted more and more in enterprise development, but it has inadvertently led to arguments between front-end students and back-end students about the granularity of API interface, more and more see:

  • “Don’t you just ask for 2 interfaces to assemble?” – Back-end students pursue service sinking and decoupling.
  • “One less HTTP, how hard is it to add an interface?” – The front end is closest to the user, so flexibility of user experience should be considered.

It boils down to one question: “Is the server interface designed for the UI or is it just a generic service?”

Of BFF

In 2015, Sam Newman proposed the Pattern: Backends For Frontends, or BFF, commonly known in China as bonding layers.

As you can see, this layer has always existed, but it is maintained by the backend students. But we know that the front end is close to the user side, the demand changes too fast, the back-end students will be very tired to maintain. In the BFF concept, the most important point is that the service should be autonomous and developed by whoever uses it, i.e. it should be maintained by the front end students.

  • Service autonomy reduces communication costs and leads to flexibility and efficiency.
  • Eat your own dog food.
  • BFF does not limit the specific technical, team selection according to their own technology stack: Java/Node/PHP/Python/Ruby…
  • General middle layer gateway based on GraphQL technology is also a good solution that has just emerged.
  • On most front-end teams, there is a tendency to choose Node.js with better ecology and more familiar syntax.

Why Node?

No, there is a need for such a middle layer, but the backend students do not want to manage (and can not manage), so most of the front-end students choose a more comfortable Node.js, that is all.

So, go to Node?

No, you need to look at it on a case-by-case basis, depending on your business scenario, and the technical architecture of your team. Remember, BFF is not limited to specific technology selection. BFF will inevitably lead to an increase in r&d costs to a certain extent and an increase in requirements for developers’ capabilities.

Express middleware

Express and Koa are currently the most popular Node-based Web development frameworks, and they have the same developers. It seems that Koa is more popular now, but there are still a lot of projects using Express, so here’s how Express middleware works. The following middleware is Express middleware.

Functionality and classification of middleware

The essence of middleware is a function that does what we want to do in the process of receiving a request and returning it. The Express documentation describes what it does like this:

Execute any code. Modify the request and response objects. Terminates the request-response loop. Call the next middleware in the stack.

The Express documentation divides them into five categories, but the principles are the same, just the usage is different:

Application-level middleware Routing level middleware error handling middleware Built-in middleware Third-party middleware

usage

Very simple.

var express = require('express')
var app = express();
app.use('/user'.function (req, res, next) {
  //TODO
  next();
});
app.listen(8080)

Copy the code

Conclusion: Front-end Oriented Node middleware can not improve our application performance, but standardize our development process, improve our work efficiency, problems can be solved faster, as for whether to use, it needs to adapt to local conditions.

Why do Internet companies start using Node.js as middleware for Web services? Are there any benefits?

The original address