This project is a backend API application template built with Node.js, Express.js, and TypeScript. It provides a structured foundation for developing robust and maintainable APIs with TypeScript's type-safety and Express's powerful routing capabilities.
- TypeScript: Leverages TypeScript for type-safety and modern JavaScript features.
- Express: Utilizes Express for building scalable and efficient APIs.
- Environment Configuration: Manages environment variables using
dotenv
. - ESLint: Ensures code quality and consistency.
- Prettier: Formats code for readability and style consistency.
- Nodemon: Enables automatic server restarts during development.
- Node.js (>= 14.x)
- npm (>= 6.x) or yarn (>= 1.x)
-
Clone the repository:
git clone https://github.com/your-username/express-typescript-template.git cd express-typescript-template
-
Install dependencies:
npm install # or yarn install
-
Create a
.env
file:cp .env.example .env
-
Set environment variables in the
.env
file:PORT=3000
To start the application in development mode with automatic restarts using Nodemon:
npm run dev
# or
yarn dev
The server will start on the port specified in the .env
file (default: 3000).
To build and start the application in production mode:
npm run build
npm start
# or
yarn build
yarn start
To lint the code using ESLint:
npm run lint
# or
yarn lint
To format the code using Prettier:
npm run format
# or
yarn format
├── src
│ ├── controllers # Define your controllers here
│ ├── middlewares # Define custom middlewares here
│ ├── routes # Define the application routes here
│ ├── services # Define your services here
│ ├── utils # Utility functions
│ ├── index.ts # Entry point of the application
├── tests # Test files
├── .env.example # Example environment variables file
├── .eslintrc.js # ESLint configuration
├── .prettierrc # Prettier configuration
├── jest.config.js # Jest configuration
├── tsconfig.json # TypeScript configuration
├── package.json # Project dependencies and scripts
└── README.md # Project documentation
-
Fork the repository:
- Navigate to the repository on GitHub and fork it to your own account.
-
Clone your forked repository:
git clone https://github.com/your-username/express-typescript-template.git cd express-typescript-template
-
Change the project name:
-
Update
package.json
with your project name and description:{ "name": "your-project-name", "version": "1.0.0", "description": "Your project description", ... }
-
-
Install dependencies:
npm install # or yarn install
-
Set up environment variables:
-
Create a
.env
file by copying the example:cp .env.example .env
-
Edit the
.env
file with your configuration:PORT=3000
-
-
Run the application in development mode:
npm run dev # or yarn dev
-
Build and run the application in production mode:
npm run build npm start # or yarn build yarn start
-
Add a new route:
-
Create a new file in
src/routes
(e.g.,example.route.ts
):import { Router } from "express"; import { exampleController } from "../controllers/example.controller"; const router = Router(); router.get("/example", exampleController); export default router;
-
Register the new route in
src/index.ts
:import express from "express"; import exampleRoute from "./routes/example.route"; const app = express(); const port = process.env.PORT || 3000; app.use(express.json()); app.use("/api", exampleRoute); app.listen(port, () => { console.log(`Server is running on port ${port}`); });
-
-
Add a new controller:
-
Create a new file in
src/controllers
(e.g.,example.controller.ts
):import { Request, Response } from "express"; export const exampleController = (req: Request, res: Response): void => { res.json({ message: "Hello from the example controller!" }); };
-
-
Add a new service:
-
Create a new file in
src/services
(e.g.,example.service.ts
):export const exampleService = (): string => { return "This is a message from the example service!"; };
-
Use the service in the controller:
import { Request, Response } from "express"; import { exampleService } from "../services/example.service"; export const exampleController = (req: Request, res: Response): void => { const message = exampleService(); res.json({ message }); };
-
If you would like to contribute to this project, please follow the guidelines in CONTRIBUTING.md.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or discussions, please open an issue in the repository.
Happy coding! 🚀