ReDoc Express Middleware
raw JSON →redoc-express is a lightweight Express middleware designed to serve interactive API documentation using the ReDoc UI from OpenAPI/Swagger specifications. The current stable version is 2.1.3, last published 6 months ago, indicating active maintenance. It provides a simple, single-middleware setup to integrate professional-grade API documentation into Node.js Express applications without requiring complex build tools. A key differentiator is its first-class TypeScript support with comprehensive type definitions, 100% test coverage, and compatibility with Node.js 6+ (ES5) and Express 4.x/5.x. It supports both OpenAPI 3.0+ and Swagger 2.0 specifications. The package focuses on ease of use, offering sensible defaults and extensive customization options via ReDoc's configuration, including built-in CSP nonce support for enhanced security.
Common errors
error Error: Cannot find module 'redoc-express' ↓
npm install redoc-express or yarn add redoc-express in your project directory. error Something went wrong... Failed to fetch ↓
specUrl points to a valid and accessible OpenAPI JSON/YAML file. Test the URL directly in your browser. Ensure your server is running and configured to serve the spec at that endpoint, and that CORS policies allow the ReDoc page to fetch it if hosted on a different domain. error YAMLException: failed to parse YAML ↓
lint command) to identify and correct any syntax or structural errors before serving it. Warnings
gotcha The `specUrl` option for `redoc-express` expects a publicly accessible URL to your OpenAPI specification, not a local file path. If your spec is a local file, you must serve it via another Express route or a static file server first. ↓
breaking Breaking changes in the underlying ReDoc library (which `redoc-express` uses) might affect the `redocOptions` configuration. While `redoc-express` API tends to be stable, the shape of the `redocOptions` object can change between major ReDoc versions. ↓
gotcha Incorrect Content Security Policy (CSP) headers on your server might block ReDoc from loading external resources or executing inline scripts, leading to a blank page or errors in the console. `redoc-express` offers `nonce` support. ↓
Install
npm install redoc-express yarn add redoc-express pnpm add redoc-express Imports
- redoc wrong
import redoc from 'redoc-express';correctimport { redoc } from 'redoc-express'; - redoc wrong
const redoc = require('redoc-express');correctconst { redoc } = require('redoc-express'); - RedocOptions
import { RedocOptions } from 'redoc-express';
Quickstart
import express from 'express';
import { redoc } from 'redoc-express';
import path from 'path';
const app = express();
const port = process.env.PORT || 3000;
// A minimal OpenAPI spec for demonstration
const openApiSpec = {
openapi: '3.0.0',
info: {
title: 'My Awesome API',
version: '1.0.0',
description: 'An example API to demonstrate redoc-express',
},
paths: {
'/hello': {
get: {
summary: 'Says hello',
responses: {
'200': {
description: 'A greeting message',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
message: { type: 'string' }
}
},
example: { message: 'Hello, World!' }
}
}
}
}
}
}
},
components: {
schemas: {}
}
};
// Serve the OpenAPI spec as a JSON file
app.get('/openapi.json', (req, res) => {
res.json(openApiSpec);
});
// Mount the redoc-express middleware
app.use(
'/docs',
redoc({
title: 'API Documentation',
specUrl: '/openapi.json', // URL where your OpenAPI spec is served
redocOptions: {
theme: {
colors: { primary: { main: '#607d8b' } },
typography: {
fontFamily: 'Montserrat, sans-serif'
}
}
}
})
);
// Basic route for the API itself
app.get('/hello', (req, res) => {
res.json({ message: 'Hello, World!' });
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log(`API Docs available at http://localhost:${port}/docs`);
console.log(`API Endpoint: http://localhost:${port}/hello`);
});