InversifyExpressHttpAdapter (Express 5)
The Express 5 HTTP adapter implementation. This adapter allows you to use InversifyJS framework with Express v5.x applications, which includes native Promise support and other modern features.
This adapter extends InversifyHttpAdapter. See the base class documentation for information about common methods like applyGlobalMiddleware, useGlobalFilters, applyGlobalGuards, useGlobalInterceptors, and useGlobalPipe.
Installation
- npm
- yarn
- pnpm
npm install @inversifyjs/http-express express inversify reflect-metadata
yarn add @inversifyjs/http-express express inversify reflect-metadata
pnpm add @inversifyjs/http-express express inversify reflect-metadata
Constructor
constructor(
container: Container,
httpAdapterOptions?: ExpressHttpAdapterOptions,
customApp?: Application,
)
Creates an Express 5 adapter instance.
Parameters
container: The Inversify container that holds controllers, guards, pipes, middleware, interceptors, and filters.httpAdapterOptions(optional): Configuration options for the adapter. See Options below.customApp(optional): A custom Express 5 application instance. If not provided, a default Express application is created.
Default Options
The adapter uses these defaults if not overridden:
{
logger: true,
useCookies: false,
useJson: true,
useText: false,
useUrlEncoded: false,
}
Methods
build
async build(): Promise<Application>
Builds and returns the Express 5 application with all configured routes, middleware, and handlers.
Returns: The Express 5 Application instance ready to be used with app.listen() or passed to http.createServer().
Options
ExpressHttpAdapterOptions
interface ExpressHttpAdapterOptions extends HttpAdapterOptions {
useCookies?: boolean;
useJson?: boolean;
useText?: boolean;
useUrlEncoded?: boolean;
}
Properties
logger(inherited): Set totrueto log route mappings on build, orfalseto disable logging, or provide a custom logger implementing theLoggerinterface from@inversifyjs/logger.useCookies: Enable cookie parsing middleware. Default:falseuseJson: Enable JSON body parsing middleware. Default:trueuseText: Enable text body parsing middleware. Default:falseuseUrlEncoded: Enable URL-encoded body parsing middleware. Default:false
Usage Example
const container: Container = new Container();
// ... bind your controllers, services, etc.
// Create the adapter with options
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
container,
{
logger: true,
useCookies: true,
useJson: true,
useUrlEncoded: true,
},
);
// Build the Express 5 application
const app: express.Application = await adapter.build();
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Express 5 Features
Express 5 includes several improvements over Express 4:
- Native Promise support in middleware and route handlers
- Improved error handling for rejected promises
- Updated dependencies and security improvements
- Better TypeScript support
The InversifyJS adapter automatically supports these features, including proper async/await error handling.
Type Parameters
The adapter is strongly typed with Express 5 types:
InversifyExpressHttpAdapter extends InversifyHttpAdapter<
Request, // Express 5 Request
Response, // Express 5 Response
NextFunction, // Express 5 NextFunction
void, // Handler return type
ExpressHttpAdapterOptions
>
See Also
- InversifyHttpAdapter (Base Class) - Common methods and configuration
- Controller - Creating route handlers
- Middleware - Adding middleware to routes
- Guards - Protecting routes with guards