Getting started
You can start with Inversify HTTP in two ways:
- Scaffold a sample app with
@inversifyjs/create-httpif you want a working project to experiment with. - Install the packages yourself if you prefer to add Inversify HTTP to an existing project or follow each step.
Scaffold a sample app
@inversifyjs/create-http generates a TypeScript HTTP app with an adapter of your choice, a Prisma-backed todo API, OpenAPI docs, and request validation.
- npm
- pnpm
- yarn
npm create @inversifyjs/http@latest my-app
pnpm create @inversifyjs/http my-app
yarn create @inversifyjs/http my-app
Generated Yarn projects use Yarn Berry. Enable Corepack (corepack enable) so the project's packageManager field is honored.
The CLI prompts for a package manager (npm, pnpm, or yarn) and an HTTP adapter (express, fastify, hono, or uwebsockets). You can also pass them as flags:
- npm
- pnpm
- yarn
npm create @inversifyjs/http@latest my-app -- --pm npm --adapter express
pnpm create @inversifyjs/http my-app --pm pnpm --adapter express
yarn create @inversifyjs/http my-app --pm yarn --adapter express
The CLI creates the project, installs dependencies, and builds it. The sample uses PostgreSQL; start the database from the generated docker-compose.yml, apply migrations, and run the app:
- npm
- pnpm
- yarn
cd my-app
docker compose up -d
npm run db:migrate
npm run serve
cd my-app
docker compose up -d
pnpm db:migrate
pnpm serve
cd my-app
docker compose up -d
yarn db:migrate
yarn serve
Then open http://localhost:3000/docs for the generated OpenAPI UI, or http://localhost:3000/status to check that the server is up. The sample includes a todo API under /todos.
When you are ready to understand each piece, start with Controllers, or follow the manual setup below.
Install the packages yourself
Install dependencies
To get started with Inversify HTTP, first install the required packages.
Begin by installing the inversify packages and reflect-metadata:
- npm
- pnpm
- yarn
npm install inversify reflect-metadata @inversifyjs/http-core
pnpm install inversify reflect-metadata @inversifyjs/http-core
yarn add inversify reflect-metadata @inversifyjs/http-core
Make sure to enable the Experimental Decorators and Emit Decorator Metadata options in your tsconfig.json.
Choose an HTTP adapter
Inversify HTTP works with several HTTP frameworks. Choose the adapter that best fits your needs:
- Express v4
- Express
- Fastify
- Hono
- uWebSockets.js
npm install @inversifyjs/http-express-v4
npm install @inversifyjs/http-express
npm install @inversifyjs/http-fastify
npm install @inversifyjs/http-hono
npm install @inversifyjs/http-uwebsockets
Configure server
After installing the packages, create a server to listen for incoming requests. Below is a basic example of setting up an Express server with Inversify HTTP.
const container: Container = new Container();
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
container,
);
const application: express.Application = await adapter.build();
application.listen(3000);
The server is ready to receive requests, but there are no handlers yet. Add a Controller to handle incoming requests.