Express Server Foundation
The Express Server Foundation is the core starting block provided by servercn. When you run:
Installation Guide
npx servercn init express-serveryou 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;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:
Core Setup
- Express.js server
- TypeScript configured
- Nodemon for development
Configuration
- Environment variable loader
- Strict environment validation
- Centralized config access
Middleware
- CORS (preconfigured)
- Cookie Parser
- JSON & URL‑encoded body parsing
- Request logging
Response Handling
- Standard API response formatter
- Consistent success and error payloads
Error Handling
- Custom API error class
- Centralized error handler middleware
- Safe production error responses
Developer Experience
- Hot reload with nodemon
- Typed request/response helpers
- Clean project structure
Standard Response Format
All API responses follow a predictable structure:
Success Response
{
"success": true,
"message": "Request successful",
"statusCode": 200,
"data": {}
}Error Response
{
"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:
- Route throws or forwards an error
- Custom
ApiErrornormalizes it - Error handler formats the response
- 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
Start development server
npm run devBuild for production
npm run buildStart production server
npm startWhy 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.