Getting Started
Prerequisitesβ
Before you begin, make sure you have:
- Node.js
>= 18.0.0β Download - npm
>= 8.0.0(included with Node.js) - A running database or Docker installed (to spin one up with
docker compose up)
Create your first projectβ
Run the following command β no global install required:
npx xpress-generator create
Or pass the project name directly to skip the first prompt:
npx xpress-generator create MyApp
Interactive promptsβ
The CLI will ask you four questions:
? Project name (PascalCase): MyApp
? What database will you use? MongoDB
? Use TypeScript? No
? Where to save the project? Current directory (.)
| Prompt | Options |
|---|---|
| Project name | Any PascalCase string (e.g. MyApp, BlogApi) |
| Database | MongoDB Β· MySQL Β· PostgreSQL Β· SQLServer |
| TypeScript | Yes / No (default: No) |
| Save location | Current directory (.) Β· Desktop Β· Downloads Β· Documents Β· Custom path |
Select "Ruta actual (.)" to create the project inside the directory you're already in. Ideal for CI workflows or when you've already created the folder.
After answering, the CLI automatically:
- Generates the full project structure
- Runs
npm init -y - Installs production dependencies
- Installs dev-runtime dependencies (
nodemon, TypeScript tools if applicable) - Installs testing dependencies (
jest,supertest,eslint, etc.) - Installs
huskyand configures pre-commit hooks - Installs migration tools (
sequelize-clifor MySQL/PostgreSQL)
This takes approximately 2β4 minutes depending on your connection.
Start developingβ
cd MyApp
Configure your environmentβ
Open .env and fill in your database credentials and secrets:
PORT=3000
NODE_ENV=development
# JWT
JWT_SECRET=replace-with-a-strong-random-secret
JWT_EXPIRES_IN=15m
REFRESH_TOKEN_SECRET=replace-with-another-strong-secret
# MongoDB example
MONGO_URI=mongodb://localhost:27017/myapp
The .env file is pre-generated with placeholder values tailored to your chosen database. See Environment Variables for the full reference.
Option A β Run with Node.js directlyβ
Make sure your database is already running, then:
npm run dev
Your server starts on http://localhost:3000.
Option B β Run with Docker (recommended)β
No database setup needed β Docker starts everything:
docker compose up
This starts both the app (with hot-reload in dev mode) and a database container. The docker-compose.yml is pre-configured for your chosen DB with healthchecks and named volumes.
For TypeScript, npm run dev uses ts-node src/server.ts. To compile for production, run npm run build which outputs to dist/.
Run migrations (MySQL / PostgreSQL / SQL Server)β
If you chose a relational database, run the initial migration to create the tables:
npm run db:migrate
This runs npx sequelize-cli db:migrate (MySQL/PostgreSQL) or node db/migrate.js (SQL Server) and creates the tables defined in db/migrations/.
MongoDB is schema-less β no migrations needed. Mongoose creates collections automatically when you insert the first document.
Verify it worksβ
Health checkβ
curl http://localhost:3000/
Expected response:
{
"status": "success",
"data": {
"message": "Β‘Bienvenido a la API de MyApp!"
}
}
Auth routesβ
# Login
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"secret123"}'
Run the testsβ
npm test
The generated project includes an integration test (tests/indexController.test.js) and a dedicated test setup file (tests/setup.js) that automatically sets NODE_ENV=test and test JWT secrets before each suite runs.
The test verifies:
GET /returns200withstatus: "success"- Unknown routes return
404withstatus: "fail" - The auth route exists and responds
Run tests in watch mode during development:
npm run test:watch
Project structure overviewβ
MyApp/
βββ src/
β βββ app.js β Express app (exported for testing)
β βββ server.js β Entry point β app.listen()
β βββ config/ β Database connection
β βββ shared/ β Shared utilities across all modules
β β βββ errors/ β AppError class
β β βββ utils/ β catchAsync, httpResponse
β β βββ constants/ β HTTP status codes & message strings
β β βββ validators/ β Zod schemas & validate middleware
β βββ middleware/ β auth.js, errorHandler.js
β βββ modules/
β βββ {name}/ β Your project's index module
β βββ auth/ β Auth module
βββ db/
β βββ migrations/ β Versioned migration files
βββ tests/
β βββ setup.js β Test environment config
β βββ indexController.test.js
βββ Dockerfile β Multi-stage build
βββ docker-compose.yml
βββ .sequelizerc β (MySQL / PostgreSQL only)
βββ jest.config.js
.xpress.jsonThis file stores project metadata (dbType, language, version). It is used by the generate:model and generate:middleware commands to know which templates to use.
Next stepsβ
- Database Guides β DB-specific setup, migrations, and Docker
- Authentication Guide β Protect your routes
- TypeScript Guide β TypeScript-specific tips
- Testing Guide β Write your own tests
- CLI Reference β All commands and options