Skip to main content

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 (.)
PromptOptions
Project nameAny PascalCase string (e.g. MyApp, BlogApi)
DatabaseMongoDB Β· MySQL Β· PostgreSQL Β· SQLServer
TypeScriptYes / No (default: No)
Save locationCurrent directory (.) Β· Desktop Β· Downloads Β· Documents Β· Custom path
Scaffold into any folder

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:

  1. Generates the full project structure
  2. Runs npm init -y
  3. Installs production dependencies
  4. Installs dev-runtime dependencies (nodemon, TypeScript tools if applicable)
  5. Installs testing dependencies (jest, supertest, eslint, etc.)
  6. Installs husky and configures pre-commit hooks
  7. Installs migration tools (sequelize-cli for 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
info

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.

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.

TypeScript projects

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

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 / returns 200 with status: "success"
  • Unknown routes return 404 with status: "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.json

This 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​