Coursera 笔记

Module 03 · Express Web Application FrameworkExpress Web 应用框架

Introduction to Middleware & Routers中间件与路由器简介

Coursera ↗

In this article, we will discuss the terms middleware and routes
在这篇文章中,我们将讨论中间件和路由的术语.

Middleware is software that sits between applications, databases, or services and allows those different technologies to communicate. It creates seamless interactions for the end user in a distributed system. 
中间件是一种位于应用程序、数据库或服务之间的软件,它允许这些不同的技术进行通信。它为分布式系统中的最终用户创建无缝的交互。

Express is a messaging framework used to handle routes and write middleware. The front end of an application uses Express to facilitate communication between components on the back end without the front-end and back-end services needing to use the same language. The front end communicates with the middleware, not directly with the back end. 
Express 是一个用于处理路由和编写中间件的通信框架。应用程序的前端使用 Express 来促进前后端组件之间的通信,而无需前端和后端服务使用相同的语言。前端与中间件通信,而不是直接与后端通信。

Messaging frameworks like Express commonly contain JSON, REST APIs, and web services. Older messaging frameworks may contain extensible markup language (XML) and simple object access protocols (SOAP) instead of JSON and REST APIs, respectively. The messaging framework provides a standardized way to handle data transfer among different applications.
消息框架如 Express 通常包含 JSON、REST API 和 Web 服务。较老的消息框架可能包含可扩展标记语言(XML)和简单对象访问协议(SOAP),分别代替 JSON 和 REST API。消息框架提供了一种标准化的方式来处理不同应用程序之间的数据传输。

A web server is an example of middleware that connects a website to a database. The web server handles the business logic and routes the data from the database based on the request. A route is the part of the code that associates an HTTP request, such as GET, POST, or DELETE, with a URL and the function that gets called that handles that URL. Routing is used in web development to split an application’s user interface based on rules identified by the browser’s URL. 
一个网络服务器是连接网站和数据库的中间件的一个例子。网络服务器处理业务逻辑并根据请求从数据库路由数据。路由是代码的一部分,它将 HTTP 请求(如 GET、POST 或 DELETE)与 URL 以及处理该 URL 的函数关联起来。路由在 Web 开发中用于根据浏览器 URL 中识别的规则分割应用程序的用户界面。

Router functions are called “middleware” collectively. Middleware is responsible for responding to an HTTP request or calling another function in the middleware chain. Express handles router functions through the Router class, such as Router.get(). As the name suggests, Router.get() handles HTTP GET requests. Other Router functions include Router.post(), Router.put(), Router.delete() in mostly the same way. These methods take two arguments, a URL path and a callback function. 
路由功能统称为“中间件”。中间件负责响应 HTTP 请求或调用中间件链中的另一个函数。Express 通过 Router 类处理路由功能,例如 Router.get()。正如其名所示,Router.get()处理 HTTP GET 请求。其他路由功能包括 Router.post()、Router.put()、Router.delete(),在大多数情况下以相同的方式进行。这些方法接受两个参数,一个 URL 路径和一个回调函数。

In addition to routing, middleware is also responsible for providing secure connections among services by encrypting and decrypting data, managing application loads by distributing traffic to different servers, and sorting or filtering data before the data is returned to the client.
除了路由,中间件还负责通过加密和解密数据,在服务之间提供安全连接,通过将流量分配到不同的服务器来管理应用程序负载,并在数据返回给客户端之前对数据进行排序或过滤。