This is a template for building backend applications using Node.js, Express, and TypeScript. It includes TypeScript configuration, Express setup, a structured project organization, and commonly used middleware.
- 📝 TypeScript for type-safe development.
- 🚀 Pre-configured Express server.
- 🔧 Easy-to-understand project structure.
Make sure you have the following installed:
- Node.js (v22 or later)
- npm
- Clone the repository:
- Install dependencies:
npm install
Scripts
Here are the detailed commands explained for this project:
- Build the project:
Compiles the TypeScript code (src/) into JavaScript and places the output in the dist/ folder. This is done using the TypeScript compiler (tsc).
npm run build
- Run app locally::
Runs the server using Node.js by executing the compiled JavaScript file located in dist/server.js. This assumes your entry point is src/server.ts (compiled to dist/server.js). The server listens on http://localhost:8080.
npm run start
- Run the development server::
Starts the server in development mode with hot-reloading. This uses nodemon to watch changes in files matching the pattern src/**/*.ts and automatically restarts the server. ts-node is used to directly execute TypeScript files without prior compilation. The server listens on http://localhost:8080.
npm run dev
- Lint the project::
Runs ESLint to analyze and report TypeScript/JavaScript code issues in the src/ directory. Automatically enforces best practices and catches potential errors.
npm run lint
The project is organized as follows:
node-express-ts-template/ ├── src/ │ ├── controllers/ # Business logic for routes │ │ └── hello-world │ ├── middlewares/ # Custom middlewares │ │ └── error-handler # Example middleware │ ├── models/ # Application models (e.g., database schemas or data structures) │ │ └── example.model.ts # Example model │ ├── routes/ # API routes │ │ └── hello-world.route.ts # Example route │ ├── app.ts # Express app setup │ ├── server.ts # Entry point of the application ├── dist/ # Compiled JavaScript output (generated) ├── .eslintrc.js # ESLint configuration ├── .gitignore # Files ignored by Git ├── .nvmrc # Node.js version configuration for nvm ├── package.json # Project dependencies and metadata ├── package-lock.json # Exact dependency versions ├── README.md # Project documentation ├── tsconfig.json # TypeScript configuration
The server runs on http://localhost:8080.
This project includes a sample route to demonstrate the structure. Access it by running the development server and opening the following URL:
GET http://localhost:8080/api/helloResponse:
{
"message": "Hello World!"
}- Node.js - JavaScript runtime for building server-side applications.
- Express - Fast, unopinionated, minimalist web framework for Node.js.
- TypeScript - Strongly typed programming language that builds on JavaScript.