@financial-times/express-web-service

中文文档
5.0.0发布于 2025/02/27MITCommonJS近 30 天下载 2831

创建Express 中间软件以服务于 / gtg,/ 健康,/ 左右,和/ ERror端点.

中文使用文档
文档目录11 节
  1. 使用量
  2. 所需资源
  3. API 文档
  4. expressWebService( [options] )
  5. 选项
  6. 实例
  7. 移徙
  8. 从v2.x.x迁移到v3.x.x.x
  9. 捐款
  10. 联系人
  11. 许可证

FT快递网络服务

创建 快递 要服务的中间软件 /__gtg, (中文(简体) ), /__health, (中文(简体) ), /__about,以及 /__error 结束点.

使用量

所需资源

运行快递网络服务需要 节点.js 20.x+ 和 (简体中文) 无。您可以安装 :

npm install @financial-times/express-web-service

你还需要使用 快递 4+. 快捷网络服务也支持 修复 稍作改动,见 实例 更多信息.

API 文档

熟悉情况 快递 在其余的API文件中假设。 您还需要使用以下模块:

const expressWebService = require("@financial-times/express-web-service")

expressWebService( [options] )

创建并返回一个服务 FT 网络服务端点的中间软件, 可用一个自定义 选项对象。 。 。 .

const app = express()

app.use(
    expressWebService({
        // options
    })
)

// or mount on a sub-path
app.use(
    "/example",
    expressWebService({
        // options
    })
)

新增线路如下:

不错的去(/__gtg(中文(简体) )

向着 FT 状态良好,排放a 200 OK 如果申请被认为健康,则予以答复 503 Service Unavailable 如果它不应该。 目的是用来作出路线决定.

总是返回 200 OK 除下列情况外 goodToGoTest 选项.

健康检查(/__health(中文(简体) )

向着 FT 健康检查标准,返回JSON数据,总结应用程序目前的健康状况.

总是返回一个 [{ ok:true }] 健康检查反应(即有效的健康检查反应,单次检查),除非 healthCheck 选项.

关于( 关于)/__about(中文(简体) )

符合(在编写本报告时,草案) FT 关于终点标准,返回描述应用程序的JSON数据,并提供所有相关仪表板的链接和支持文件资源.

有关终点发布的信息来自 about 选项。 这个 _hostname 财务报告和审定财务报表 appVersion 字段将自动覆盖,如果在通过 about 选项.

错误( E)/__error(中文(简体) )

此端点只是丢出一个 JavaScript 错误, 而不尝试处理它 。 这对于测试您的应用程序处理 prod 中的运行时间错误的方式很有用, 既包括报告错误( 例如记录或聚合工具), 也包括向终端用户提交适当的 UI .

选项

现有备选方案如下:

  • manifestPath: (可选) 应用程序路径 package.json 文档。 这将用来填充 appVersion 财务报告和审定财务报表 dateDeployed 属性 /__about 如果未明确指明,则结束点
  • about: (可选) 含有标准运行本属性的对象,定义如下: ft 关于终点标准,或者一个解决包含这些属性的对象的保证
  • goodToGoTest: (可选) 用于表示服务状态的功能,该功能应返回一个解决了的许诺 true 以显示积极的状态 false 以表示对去状态的负面效果
  • healthCheck: (可选) 用于生成结构化健康检查信息的函数,该函数应返回用一系列健康检查对象解决的许诺
  • routes: (可选)要安装的路由数组. 可能值为 health, (中文(简体) ), gtg, (中文(简体) ), about 财务报告和审定财务报表 error默认为 ["health", "gtg", "about"]

实例

基本实例:

const express = require('express');
const expressWebService = require('@financial-times/express-web-service');

const app = express();

app.use(expressWebService({
    manifestPath: `${__dirname}/package.json`,
    about: {
        "schemaVersion": 1,
        "name": "build-service",
        "purpose": "Front end build process as a service.  Fetches specified Origami components from git, runs Origami build process, and returns the resulting CSS or JS bundle over HTTP.",
        "audience": "public",
        "primaryUrl": "https://origami-build.ft.com",
        "serviceTier": "gold"
    }
}));

GoodTo Go逻辑和健康检查逻辑示例 :

app.use(expressWebService({
    manifestPath: `${__dirname}/package.json`,
    about: {
        "schemaVersion": 1,
        "name": "build-service",
        "purpose": "Front end build process as a service.  Fetches specified Origami components from git, runs Origami build process, and returns the resulting CSS or JS bundle over HTTP.",
        "audience": "public",
        "primaryUrl": "https://origami-build.ft.com",
        "serviceTier": "gold"
    },
    goodToGoTest: function() {
        return new Promise(function(resolve, reject) {
            resolve(isApplicationHappy());
        });
    },
    healthCheck: function() {
        // You might have several async checks that you need to perform or
        // collect the results from, this is a really simplistic example
        return new Promise(function(resolve, reject) {
            resolve([
                {
                    name: "Database TCP connectivity",
                    ok: false,
                    severity: 2,
                    businessImpact: "Article pages will not be available",
                    technicalSummary: "The database is dead",
                    panicGuide: "Check the health status of the database at host <database host>",
                    checkOutput: "tcp connect failed after 10 seconds on destination port 3306 - destination unreachable",
                    lastUpdated: new Date().toISOString()
                }
            ]);
        });
    }
}));

使用实例 修复 而非快报 :

const expressWebService = require("@financial-times/express-web-service")
const restify = require("restify")

const server = restify.createServer()

// Because Restify doesn't mount middleware for routes that don't exist,
// we have to explicitly define the required routes
server.get(
    /^\/__(about|gtg|health)$/,
    expressWebService({
        // config
    })
)

移徙

此迁移指南旨在帮助您在Express Web Service的主要版本之间迁移.

从v2.x.x迁移到v3.x.x.x

这个库2和3版本之间有一些大的变化. 首先,该单元已经从 express-ftwebservice 改为 @financial-times/express-web-service。 。 。 .

整个API被更新为使用Express中件:

const express = require("express")
const expressWebService = require("@financial-times/express-web-service")
const app = express()
const options = {}

// Old
expressWebService(app, options)

// New
app.use(expressWebService(options))

同时 about 选项现在可以接受对象(匹配版本2),也可以接受解决对象的许诺。 这使得您能够同步生成您的信息 :

app.use(
    expressWebService({
        about: {
            name: "My App",
        },
    })
)
// or
app.use(
    expressWebService({
        about: new Promise((resolve, reject) => {
            // do something async
            resolve(retrievedAboutInfo)
        }),
    })
)

捐款

这个模块有一套完整的单元测试,通过ESLint验证. 您可以在打开拉出请求前使用下列命令来检查您的代码 .

make verify  # verify JavaScript code with ESLint
make test    # run the unit tests and check coverage

联系人

如果您对本单元有任何问题或评论,或需要帮助使用,请 提出问题访问 #origami - 支持 或电子邮件 折纸支持。 。 。 .

许可证

本软件由金融时报根据以下规定出版: 麻省理工学院许可证。 。 。 .

版本与发布记录

@financial-times/express-web-service 共发布 8 个版本,首次发布于 2017/01/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.0latest2025/02/271.7 年前
4.0.02023/01/183 年前
3.4.42018/01/108 年前
3.4.32017/12/018 年前
3.4.22017/11/158 年前
3.4.12017/11/148 年前

关于 @financial-times/express-web-service 的常见问题

@financial-times/express-web-service 是什么?

创建Express 中间软件以服务于 / gtg,/ 健康,/ 左右,和/ ERror端点.

如何安装 @financial-times/express-web-service?

在项目目录运行 npm install @financial-times/express-web-service,或使用 pnpm add / yarn add / bun add。

@financial-times/express-web-service 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/financial-times__express-web-service 是否存在。

@financial-times/express-web-service 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@financial-times/express-web-service 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@financial-times/express-web-service 可以商用吗?

@financial-times/express-web-service 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@financial-times/express-web-service 的下载量有多少?

根据 npm 官方下载统计,@financial-times/express-web-service 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,831 次,日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@financial-times/express-web-service 还在维护吗?

最新版本 5.0.0 发布于 2025/02/27(1.7 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/01/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。