Skip to content

josueperezparejo/backend-template-express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧱 Backend Template – Express + TypeScript + Sequelize

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.

πŸ“¦ Features

  • βš™οΈ 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

πŸš€ Getting Started

1. Clone the repo and install dependencies

git clone https://github.com/your-username/backend-template-express.git
cd backend-template-express
npm install

2. Create your .env file

# 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

3. Project Structure

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

4. Scripts

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

5. Sequelize CLI Scripts

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.

6. Database Handling

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);
      }
    }
  },
};

7. Middleware & Utilities

  • 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)

8. API Example

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

9. Clean Build

To remove old builds before creating a new one:

npm run clean
npm run build

10. Production Notes

  • 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.

Tech Stack

  • Express.js

  • TypeScript

  • PostgreSQL + Sequelize

  • dotenv for environment config

  • helmet, morgan, cors for middleware

πŸ› οΈ Migrations Guide

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.

Creating a New Migration

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.

Running Migrations (Development)

npm run db:migrate

Reverting the Last Migration

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

Creating and Running Seeders

Create a seeder file:

npx sequelize-cli seed:generate --name <seed-name>

Running Seeders (Dummy/Test Data)

npm run db:seed

Make sure you have seeder files inside the seeders/ folder.

Running Migrations in Development

npm run db:migrate
npm run db:seed

Running Migrations in Production

When working in production, carefully apply migrations to the live database:

npm run db:migrate:prod
npm run db:seed:prod

⚠️ Use these commands with caution in production environments.

Author

GitHub Linkedin

About

A backend template using Express, TypeScript, and Sequelize for rapid development.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published