Swagger Express Fixed Middleware
raw JSON →swagger-express-fixed is a specialized fork of the legacy apigee-127/swagger-express library, designed to provide critical compatibility fixes for Node.js environments beyond version 10. The package, currently at version 1.0.2, aims to enable older Express applications that rely on the original `swagger-express` to run on modern Node.js runtimes without necessitating a complete rewrite to contemporary OpenAPI tooling like `swagger-ui-express` and `swagger-jsdoc`. It achieves this by integrating an updated `swagger-node-runner` (or its fixed variant), which now utilizes the `Sway` engine for robust API validation. This fork prioritizes stability and bug fixes over new feature development, making its release cadence infrequent and focused on critical maintenance for existing projects.
Common errors
error TypeError: Cannot read property 'swaggerUi' of undefined ↓
swagger-express-fixed and that its internal dependencies are correctly resolved. Verify package-lock.json and node_modules structure. If this error persists, consider a clean npm install. error YAMLSemanticError: (in ..., at line x, column y) ↓
swagger.yaml file for syntax errors, improper indentation, or schema violations. Use an OpenAPI/Swagger editor or linter (e.g., swagger-cli validate) to validate your YAML file. error Error: The 'apis' array is empty. No files were found matching the provided paths. ↓
apis array in your config object (or swagger.yaml) contains the correct relative or absolute paths to your API definition files. Ensure file names and extensions match and are accessible from the appRoot. error Failed to load API definition. Possible cross-origin (CORS) issue? ↓
Access-Control-Allow-Origin headers. Ensure the swaggerURL and basePath are correctly configured in swagger.init options. Warnings
breaking The original `apigee-127/swagger-express` is known to be incompatible with Node.js versions greater than 10. This `_fixed` fork specifically addresses these runtime issues. Attempting to use the unpatched version on newer Node.js runtimes will result in crashes or unexpected behavior. ↓
breaking The underlying `swagger-node-runner` dependency, used by this package, transitioned to a 'Sway-based release' in version `0.6.0`. This change introduces a new validation engine, which might alter API behavior or strictness compared to older versions, potentially breaking existing API contracts if not carefully reviewed. ↓
gotcha Cross-Origin Resource Sharing (CORS) issues are common when serving Swagger UI, especially if the API and the UI are on different origins, or if the API definitions themselves have CORS-related misconfigurations. This can lead to the Swagger UI failing to load API definitions or make requests. ↓
gotcha This package is primarily built for Swagger/OpenAPI 2.0 specifications. While OpenAPI 3.x is the current standard, using a 3.x definition directly with this tool might lead to parsing errors or unexpected behavior as it may not fully support the newer specification structure (e.g., `components` object, `requestBody`). ↓
Install
npm install swagger-express-fixed yarn add swagger-express-fixed pnpm add swagger-express-fixed Imports
- create wrong
import { create } from 'swagger-express-fixed';correctconst swagger = require('swagger-express-fixed'); // Then use: swagger.create(options, callback) - init wrong
import { init } from 'swagger-express-fixed';correctconst swagger = require('swagger-express-fixed'); app.use(swagger.init(app, options));
Quickstart
const express = require('express');
const path = require('path');
const swagger = require('swagger-express-fixed');
const app = express();
const port = 3000;
const config = {
appRoot: __dirname, // required for swagger-node-runner
swaggerFile: path.resolve(__dirname, './api/swagger.yaml') // Path to your API definition
};
/* Example swagger.yaml content (place in ./api/swagger.yaml)
---
swagger: '2.0'
info:
title: My Express API
version: '1.0.0'
paths:
/hello:
get:
summary: Returns a greeting
responses:
200:
description: A simple greeting
schema:
type: string
*/
swagger.create(config, function(err, swaggerMiddleware) {
if (err) { throw err; }
// Add swagger-express-fixed middleware to your Express app
app.use(swaggerMiddleware.swaggerSecurity({})
app.use(swaggerMiddleware.swaggerMetadata());
app.use(swaggerMiddleware.swaggerValidator());
app.use(swaggerMiddleware.swaggerRouter({ useStubs: true }));
app.use(swaggerMiddleware.swaggerUi());
// Custom error handler (optional)
app.use(function(err, req, res, next) {
if (err.statusCode) {
res.status(err.statusCode).json(err.message);
} else {
res.status(500).json('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
console.log(`Swagger UI available at http://localhost:${port}/docs`);
});
});