Express Server Foundation

The Express Server Foundation is the core starting block provided by servercn. When you run:

Installation Guide

npx servercn init express-server

you get a production‑ready Express.js server with a clean, scalable foundation and sensible defaults. This foundation eliminates repetitive setup and lets you focus immediately on business logic.

This is not a framework. It is a well‑structured boilerplate you fully own.

Example Server

import express, { Express, Request, Response } from "express";
import "dotenv/config";
import cors from "cors";
import helmet from "helmet";
import cookieParser from "cookie-parser";
import morgan from "morgan";
import { notFoundHandler } from "./middlewares/not-found-handler";
import { errorHandler } from "./middlewares/error-handler";
import healthRoutes from "./routes/health.routes";
import env from "./configs/env";
 
const app: Express = express();
 
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
  cors({
    origin: env.CORS_ORIGIN,
    credentials: true
  })
);
app.use(helmet());
app.use(cookieParser());
app.use(morgan(env.NODE_ENV === "development" ? "dev" : "combined"));
 
// Routes
 
app.get("/", (req: Request, res: Response) => {
  res.redirect("/api/v1/health");
});
 
app.use("/api/v1/health", healthRoutes);
 
// Not found handler (should be after routes)
app.use(notFoundHandler);
 
// Global error handler (should be last)
app.use(errorHandler);
 
export default app;

Official Docs

What This Foundation Solves

Most Express projects repeat the same setup:

  • Environment configuration
  • CORS and cookie handling
  • Standard API response format
  • Centralized error handling
  • TypeScript configuration
  • Development tooling

The Express Server Foundation standardizes these concerns using battle‑tested patterns.

What You Get Out of the Box

After initialization, your project includes:

  • Express.js server
  • TypeScript configured
  • Nodemon for development
  • Environment variable loader
  • Strict environment validation
  • Centralized config access
  • CORS (preconfigured)
  • Cookie Parser
  • JSON & URL‑encoded body parsing
  • Request logging
  • Standard API response formatter
  • Consistent success and error payloads
  • Custom API error class
  • Centralized error handler middleware
  • Safe production error responses
  • Hot reload with nodemon
  • Typed request/response helpers
  • Clean project structure

Standard Response Format

All API responses follow a predictable structure:

{
  "success": true,
  "message": "Request successful",
  "statusCode": 200,
  "data": {}
}
{
  "success": false,
  "message": "Invalid request",
  "statusCode": 400,
  "errors": []
}

This makes frontend integration consistent and predictable.

Environment Configuration

Environment variables are loaded and validated at startup.

PORT='5000'
NODE_ENV='development'
LOG_LEVEL='info'
CORS_ORIGIN='http://localhost:3000'

If a required variable is missing, the server fails fast instead of crashing later.

Error Handling Strategy

The foundation includes a centralized error pipeline:

  1. Route throws or forwards an error
  2. Custom ApiError normalizes it
  3. Error handler formats the response
  4. Stack traces are hidden in production

This ensures safe error exposure in production.

CORS & Cookies

CORS is preconfigured with:

  • Credential support
  • Environment‑based origin control

Cookies are handled using cookie-parser with TypeScript support.

Development Workflow

npm run dev
npm run build
npm start

Why This Is a Foundation (Not a Template)

This setup intentionally excludes:

  • Authentication
  • Database configuration
  • Business logic

Those are added using servercn components and blocks.

The foundation gives you:

  • A clean starting point
  • Proven defaults
  • Freedom to evolve

Extending the Foundation

Once initialized, you can add:

  • JWT Authentication block
  • Request validation (Zod)
  • Logging and monitoring
  • Database adapters

Each addition builds on top of this foundation without rewriting core setup.

When to Use Express Server Foundation

Use this foundation when:

  • Starting a new Express API
  • Building a production REST backend
  • You want full control over your server
  • You want consistent structure across projects

Summary

The Express Server Foundation is the recommended starting point for any Express‑based backend using servercn.

It removes setup friction while keeping your architecture explicit, understandable, and production‑ready.

Installation

npx servercn init express-server