从 Express.js 转换到 Azure FunctionsShifting from Express.js to Azure Functions

Express.js 是 Web 开发人员最欢迎的 Node.js 框架之一,并且仍是构建提供 API 终结点的应用的绝佳选择。Express.js is one of the most popular Node.js frameworks for web developers and remains an excellent choice for building apps that serve API endpoints.

将代码迁移到无服务器体系结构时,重构 Express.js 终结点会影响以下方面:When migrating code to a serverless architecture, refactoring Express.js endpoints affects the following areas:

  • 中间件:Express.js 具有一系列强大的中间件。Middleware: Express.js features a robust collection of middleware. 鉴于 Azure Functions 和 Azure API 管理功能,许多中间件模块不再需要用到。Many middleware modules are no longer required in light of Azure Functions and Azure API Management capabilities. 确保迁移终结点之前可以复制或替换必要中间件处理的任何逻辑。Ensure you can replicate or replace any logic handled by essential middleware before migrating endpoints.

  • 不同的 API:Azure Functions 和 Express.js 中用于处理请求和响应的 API 有所不同。Differing APIs: The API used to process both requests and responses differs among Azure Functions and Express.js. 下面的示例详细介绍必需的更改。The following example details the required changes.

  • 默认路由: 默认情况下,Azure Functions 终结点公开在 api 路由下。Default route: By default, Azure Functions endpoints are exposed under the api route. 路由规则可通过 host.json 文件中的 routePrefix 进行配置。Routing rules are configurable via routePrefix in the host.json file.

  • 配置和约定:Functions 应用使用 function.json 文件来定义 HTTP 谓词和安全策略,并可以配置函数的输入和输出Configuration and conventions: A Functions app uses the function.json file to define HTTP verbs, define security policies, and can configure the function's input and output. 默认情况下,包含函数文件的文件夹名称定义终结点名称,但可以通过 function.json 文件中的 route 属性更改该名称。By default, the folder name that which contains the function files defines the endpoint name, but you can change the name via the route property in the function.json file.

提示

有关详细信息,请参阅交互式教程使用 Azure Functions 将 Node.js 和 Express API 重构为无服务器 APILearn more through the interactive tutorial Refactor Node.js and Express APIs to Serverless APIs with Azure Functions.

示例Example

Express.jsExpress.js

下面的示例演示了典型的 Express.js GET 终结点。The following example shows a typical Express.js GET endpoint.

// server.js
app.get('/hello', (req, res) => {
  try {
    res.send("Success!");
  } catch(error) {
    const err = JSON.stringify(error);
    res.status(500).send(`Request error. ${err}`);
  }
});

/hello 发送 GET 请求时,将返回包含 SuccessHTTP 200 响应。When a GET request is sent to /hello, an HTTP 200 response containing Success is returned. 如果终结点遇到错误,响应返回包含错误详细信息的 HTTP 500If the endpoint encounters an error, the response is an HTTP 500 with the error details.

Azure FunctionsAzure Functions

Azure Functions 将配置和代码文件组织到每个函数的单个文件夹中。Azure Functions organizes configuration and code files into a single folder for each function. 默认情况下,该文件夹的名称指示函数名称。By default, the name of the folder dictates the function name.

例如,名为 hello 的函数具有包含以下文件的文件夹。For instance, a function named hello has a folder with the following files.

| - hello
|  - function.json
|  - index.js

以下示例实现与上述 Express.js 终结点相同的结果,但这是通过 Azure Functions 完成的。The following example implements the same result as the above Express.js endpoint, but with Azure Functions.

// hello/index.js
module.exports = async function (context, req) {
  try {
    context.res = { body: "Success!" };
  } catch(error) {
    const err = JSON.stringify(error);
    context.res = {
      status: 500,
      body: `Request error. ${err}`
    };
  }
};

移动到 Functions 时,将进行以下更改:When moving to Functions, the following changes are made:

  • 模块: 以 JavaScript 模块的形式实现函数代码。Module: The function code is implemented as a JavaScript module.

  • 上下文和响应对象context 让你可以与函数的运行时进行通信。Context and response object: The context allows you to communicate with the Function's runtime. 在上下文中,可以读取请求数据并设置函数的响应。From the context, you can read request data and set the function's response. 同步代码需要调用 context.done() 来完成执行,而 asyc 函数隐式解析请求。Synchronous code requires you to call context.done() to complete execution, while asyc functions resolve the request implicitly.

  • 命名约定:默认情况下,用于包含 Azure Functions 文件的文件夹名称用作终结点名称(可以在 function.json 中进行重写)。Naming convention: The folder name used to contain the Azure Functions files is used as the endpoint name by default (this can be overridden in the function.json).

  • 配置:在 function.json 文件中定义 HTTP 谓词,如 POSTPUTConfiguration: You define the HTTP verbs in the function.json file such as POST or PUT.

以下 function.json 文件保存函数的配置信息。The following function.json file holds configuration information for the function.

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

通过在 methods 数组中定义 get,该函数可用于 HTTP GET 请求。By defining get in the methods array, the function is available to HTTP GET requests. 如果希望 API 接受支持 POST 请求,还可以将 post 添加到数组中。If you want to your API to accept support POST requests, you can add post to the array as well.

后续步骤Next steps