@apidevtools/swagger-express-middleware

中文文档
4.0.2发布于 2020/08/01MITCommonJS近 30 天下载 6330

Swagger middleware and mocks for Express

此软件包已被标记为弃用

This package has been abandoned. Please find an actively maintained alternative on https://openapi.tools/

原始使用文档
文档目录7 节
  1. Swagger 2.0 middleware and mocks for Express.js
  2. Features
  3. Installation and Use
  4. Samples & Walkthroughs
  5. Contributing
  6. License
  7. Big Thanks To

Swagger Express Middleware

Swagger 2.0 middleware and mocks for Express.js

Cross-Platform Compatibility Build Status

Coverage Status Tested on APIs.guru Dependencies

npm License Buy us a tree

Features

  • Supports Swagger 2.0 specs in JSON or YAML
    Swagger Express Middleware uses Swagger-Parser to parse, validate, and dereference Swagger files. You can even split your spec into multiple different files using $ref pointers.

  • Thoroughly tested
    Over 1,000 unit tests and integration tests with 100% code coverage. Tested on over 1,500 real-world APIs from Google, Microsoft, Facebook, Spotify, etc. All tests are run on Mac, Linux, and Windows using all LTS versions of Node.

  • Mock middleware
    Fully-functional mock implementations for every operation in your API, including data persistence, all with zero code! This is a great way to test-drive your API as you write it, or for quick demos and POCs. You can even extend the mock middleware with your own logic and data to fill in any gaps.

  • Metadata middleware
    Annotates each request with all the relevant information from the Swagger definition. The path, the operation, the parameters, the security requirements - they're all easily accessible at req.swagger.

  • Parse Request middleware
    Parses incoming requests and converts everything into the correct data types, according to your Swagger API definition.

  • Validate Request middleware
    Ensures that every request complies with your Swagger API definition, or returns the appropriate HTTP error codes if needed. Of course, you can catch any validation errors and handle them however you want.

  • CORS middleware
    Adds the appropriate CORS headers to each request and automatically responds to CORS preflight requests, all in compliance with your Swagger API definition.

  • Files middleware
    Serves the Swagger API file(s) in JSON or YAML format so they can be used with front-end tools like Swagger UI, Swagger Editor, and Postman.

Installation and Use

Install using npm.

npm install @apidevtools/swagger-express-middleware

Then use it in your Node.js script like this:

const express = require('express');
const createMiddleware = require('@apidevtools/swagger-express-middleware');

let app = express();

createMiddleware('PetStore.yaml', app, function(err, middleware) {
    // Add all the Swagger Express Middleware, or just the ones you need.
    // NOTE: Some of these accept optional options (omitted here for brevity)
    app.use(
        middleware.metadata(),
        middleware.CORS(),
        middleware.files(),
        middleware.parseRequest(),
        middleware.validateRequest(),
        middleware.mock()
    );

    app.listen(8000, function() {
        console.log('The PetStore sample is now running at http://localhost:8000');
    });
});

Samples & Walkthroughs

Swagger Express Middleware comes two samples that use the Swagger Pet Store API.

Sample 1

This sample demonstrates the most simplistic usage of Swagger Express Middleware. It simply creates a new Express Application and adds all of the Swagger middleware without changing any options, and without adding any custom middleware.

Sample 2

This sample demonstrates a few more advanced features of Swagger Express Middleware, such as setting a few options, initializing the mock data store, and adding custom middleware logic.

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/swagger-express-middleware.git

  2. Install dependencies
    npm install

  3. Run the tests
    npm test

  4. Run the sample app
    npm start

License

Swagger Express Middleware is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

Travis CI SauceLabs Coveralls

版本与发布记录

@apidevtools/swagger-express-middleware 共发布 6 个版本,首次发布于 2020/03/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.2latest2020/08/016 年前
4.0.12020/07/196 年前
4.0.02020/03/256 年前
3.0.12020/03/156 年前
3.0.02020/03/156 年前
2.0.52020/03/156 年前

关于 @apidevtools/swagger-express-middleware 的常见问题

@apidevtools/swagger-express-middleware 是什么?

Swagger middleware and mocks for Express注意:该包已被作者标记为弃用——This package has been abandoned. Please find an actively maintained alternative on https://openapi.tools/

如何安装 @apidevtools/swagger-express-middleware?

在项目目录运行 npm install @apidevtools/swagger-express-middleware,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:express(4.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apidevtools/swagger-express-middleware 支持 TypeScript 吗?

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

@apidevtools/swagger-express-middleware 支持 ESM 还是 CommonJS?

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

@apidevtools/swagger-express-middleware 需要什么版本的 Node.js?

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

@apidevtools/swagger-express-middleware 可以商用吗?

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

@apidevtools/swagger-express-middleware 的下载量有多少?

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

@apidevtools/swagger-express-middleware 还在维护吗?

最新版本 4.0.2 发布于 2020/08/01(6 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/15。作者已将其标记为弃用,新项目建议寻找替代方案。