This is a clean, scalable backend template built with Express.js, TypeScript, and Sequelize. It includes a class-based server structure, PostgreSQL database integration, Sequelize CLI support for migrations and seeds, and essential middlewares for security and logging.
- βοΈ Express 5 with TypeScript
- π§ Sequelize ORM for PostgreSQL with CLI support (migrations, seeds)
- ποΈ Class-based server architecture
- π± Environment-specific database configs via
.env
- πͺͺ Security with
helmet
- π Logging with
morgan
- π Live reload with
tsx watch
- β
Ready for input validation with
express-validator
- π CORS support
- π§Ή Clean build process using
rimraf
git clone https://github.com/your-username/backend-template-express.git
cd backend-template-express
npm install
# App Ports
APP_PORT_DEV=3000
APP_PORT_PROD=8000
# PostgreSQL_DEV
POSTGRES_DEV_DB=example_dev_db
POSTGRES_DEV_USER=example_dev_user
POSTGRES_DEV_PASSWORD=example_dev_password
POSTGRES_DEV_PORT=5432
POSTGRES_DEV_HOST=127.0.0.1
# PostgreSQL_PROD
POSTGRES_PROD_DB=example_prod_db
POSTGRES_PROD_USER=example_prod_user
POSTGRES_PROD_PASSWORD=example_prod_password
POSTGRES_PROD_PORT=5432
POSTGRES_PROD_HOST=127.0.0.1
# PgAdmin
PGADMIN_DEFAULT_EMAIL=example_admin@example.com
PGADMIN_DEFAULT_PASSWORD=example_password
PGADMIN_PORT=8080
src/
βββ config/ # DB credentials loaded from .env
βββ controllers/ # Business logic for each route
βββ database/ # Sequelize setup (authenticate, sync, instance)
βββ middlewares/ # Middlewares for validation, errors, etc.
βββ models/ # Sequelize model definitions
βββ routes/ # Express routes grouped by resource
βββ services/ # Optional: logic separated from controllers
βββ validators/ # express-validator schemas
βββ server.ts # Class-based server configuration
βββ index.ts # App entry point
Script | Description |
---|---|
npm run dev |
Starts the server in development mode with tsx watch |
npm run build |
Compiles TypeScript into JavaScript inside dist/ |
npm start |
Builds and starts the server in production mode |
npm run clean |
Removes the dist/ folder |
db:migrate , db:seed |
Sequelize CLI commands for managing DB migrations/seeds |
Command | Environment | Description |
---|---|---|
npm run db:migrate |
Development | Run all pending migrations |
npm run db:migrate:undo |
Development | Revert the last migration |
npm run db:seed |
Development | Seed the database with dummy data |
npm run db:migrate:prod |
Production | Run migrations on production DB |
npm run db:seed:prod |
Production | Seed production database |
π‘ Sequelize reads the environment from NODE_ENV and uses matching .env variables.
const database = {
sequelize,
Sequelize,
async authenticate() {
try {
await sequelize.authenticate();
console.log("β
Database connected");
} catch (error) {
console.error("β Unable to connect to the database");
process.exit(1);
}
},
async sync() {
if (process.env.NODE_ENV === "development") {
try {
await sequelize.sync({ alter: true });
console.log("β
Models synchronized");
} catch (error) {
console.error("β Error synchronizing models");
process.exit(1);
}
}
},
};
-
helmet β sets secure HTTP headers
-
morgan β logs HTTP requests in development
-
cors β enables CORS
-
express.json and express.urlencoded β parses incoming requests
-
express-validator β validates incoming data (optional)
A basic example route under /api/tasks might look like:
Method Endpoint Description
GET /api/tasks List all tasks
GET /api/tasks/:id Get task by ID
POST /api/tasks Create a new task
PUT /api/tasks/:id Update an existing task
DELETE /api/tasks/:id Delete a task
To remove old builds before creating a new one:
npm run clean
npm run build
-
Avoid using sequelize.sync({ alter: true }) in production.
-
Use migrations (db:migrate:prod) instead for consistent DB changes.
-
Make sure .env variables for production are set securely.
-
Express.js
-
TypeScript
-
PostgreSQL + Sequelize
-
dotenv for environment config
-
helmet, morgan, cors for middleware
This project uses Sequelize CLI to manage database migrations. Below is a typical workflow for creating, running, and reverting migrations in both development and production environments.
npx sequelize-cli migration:generate --name <migration-name>
π§ Replace with a descriptive name like create-tasks-table.
This will generate a new file in the migrations/ directory with a boilerplate structure to define your schema changes.
npm run db:migrate
npm run db:migrate:undo
This command applies all pending migrations to your local development database.
This will undo the last executed migration.
To revert all migrations (e.g., for resetting the development database):
npx sequelize-cli db:migrate:undo:all
Create a seeder file:
npx sequelize-cli seed:generate --name <seed-name>
npm run db:seed
Make sure you have seeder files inside the seeders/ folder.
npm run db:migrate
npm run db:seed
When working in production, carefully apply migrations to the live database:
npm run db:migrate:prod
npm run db:seed:prod