A robust and scalable Node.js server template built with TypeScript, Express, and modern development tools. This template is designed to help you quickly set up a production-ready Node.js server with TypeScript.
- TypeScript: Strongly-typed JavaScript for better development experience.
- Express: Fast and minimalist web framework for Node.js.
- Morgan: HTTP request logging for monitoring incoming requests.
- Dotenv: Environment variable management.
- Nodemon: Automatic server restart during development.
- Modular Structure: Organized folder structure for scalability.
- Environment Template: Includes a
.env.examplefile for easy setup.
Before you begin, ensure you have the following installed:
Clone this repository to your local machine:
git clone https://github.com/your-username/nodejs-ts-server.git
cd nodejs-ts-serverInstall the required dependencies:
npm installRun the setup script to create a .env file from the provided .env.example:
npm run setupOpen the .env file and replace the placeholders with your actual values:
PORT=3000
NODE_ENV=developmentStart the server in development mode:
npm run devThe server will start at http://localhost:3000.
nodejs-ts-server/
├── src/
│ ├── controllers/ # Route controllers
│ ├── routes/ # Route definitions
│ ├── services/ # Business logic
│ ├── utils/ # Utility functions
│ ├── app.ts # Express app setup
│ └── server.ts # Server entry point
├── .env.example # Environment variables template
├── .env # Environment variables (ignored by Git)
├── .gitignore # Files to ignore in Git
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── nodemon.json # Nodemon configuration
└── README.md # Project documentation
npm run dev: Start the server in development mode usingnodemonandts-node.npm run build: Compile TypeScript files to JavaScript in thedistfolder.npm start: Start the server in production mode using the compiled JavaScript files.npm run setup: Create a.envfile from.env.example.
To add a new route, follow these steps:
-
Create a new controller in the
src/controllers/folder.// src/controllers/exampleController.ts import { Request, Response } from "express"; export const getExample = (req: Request, res: Response) => { res.json({ message: "This is an example route" }); };
-
Define the route in the
src/routes/folder.// src/routes/exampleRoutes.ts import express from "express"; import { getExample } from "../controllers/exampleController"; const router = express.Router(); router.get("/example", getExample); export default router;
-
Register the route in
src/app.ts.import exampleRoutes from "./routes/exampleRoutes"; // Add this line after other middleware app.use("/api", exampleRoutes);
Now, the new route will be available at http://localhost:3000/api/example.
The project uses environment variables for configuration. To set up your environment:
-
Rename the
.env.examplefile to.env:cp .env.example .env
-
Open the
.envfile and replace the placeholders with your actual values:PORT=3000 NODE_ENV=development
The following environment variables are used in this project:
PORT: The port on which the server will run (default:3000).NODE_ENV: The environment mode (developmentorproduction).
HTTP request logging is handled by morgan. In development mode, logs are printed to the console in the dev format. You can customize the logging format or output as needed.
Contributions are welcome! If you find a bug or want to add a feature, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Express for the web framework.
- TypeScript for adding type safety.
- Morgan for HTTP request logging.
Enjoy building your Node.js server with TypeScript! 🚀