getting-started
π Getting Started with express-cargoβ
This document provides a minimum configuration guide for quickly experiencing express-cargo in a TypeScript + Express environment. The examples are based on pnpm + Node.js 18 or later.
1. Requirementsβ
- Node.js 18 or later (LTS recommended)
Package Manager: npm | yarn | pnpm β This document uses pnpm.
Since express-cargo uses Decorator + Metadata, TypeScript is recommended.
2. Installing Node.jsβ
Install the LTS version appropriate for your OS from the official website below.
- Node.js Official Download https://nodejs.org
Verify installation:
node -v
pnpm -v
If successful, the version will be displayed.
3. Create a new project (pnpm)β
pnpmβ
mkdir express-cargo-example
cd express-cargo-example
pnpm init
4. Set up the TypeScript environmentβ
4-1. Install TypeScript and development dependenciesβ
pnpm add -D typescript ts-node @types/node
4-2. Create tsconfig.jsonβ
pnpm tsc --init
4-3. Recommended tsconfig settingsβ
{
"compilerOptions": {
...,
"experimentalDecorators": true, // express-cargo required
"emitDecoratorMetadata": true, // use type metadata
...
},
...
}
β οΈ experimentalDecorators, emitDecoratorMetadata Without it, validation/transformation in express-cargo will not work.
5. Install express & express-cargoβ
pnpm add express express-cargo
pnpm add -D @types/express
pnpm add reflect-metadata
6. Base server + express-cargo settingsβ
6-1. src/app.tsβ
import express from 'express'
import { bindingCargo, getCargo, Body, Query, Header, Params, Min, Max, Equal, NotEqual, Prefix, Suffix } from 'express-cargo'
import errorHandlerRouter from './errorHandler'
const app = express()
const port = process.env.PORT || 3000
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(errorHandlerRouter)
app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
class ExampleRequest {
@Body() // Extract fields from the request body
@Equal('1') // If the value is not "1", a validation error occurs
id!: string
}
app.post('/example', bindingCargo(ExampleRequest), (req, res) => { // bindingCargo(Class): Request β DTO conversion + validation
const cargo = getCargo<ExampleRequest>(req) // Return a validated type-safe object
res.json(cargo)
})
7. Runβ
npm run dev
test request:
POST http://localhost:3000/example
Content-Type: application/json
{
"id": "1"
}
response:
{ "id": "1" }
- β
"id": "2"β Validation Error occurs