A solid foundation for building backend services with Node.js and TypeScript. This repository serves as a template for my future backend projects, powered by a modern and efficient stack.
- TypeScript: Code with types for more robust and maintainable applications.
- Fastify: High-performance, low-overhead web framework.
- Mongoose: Elegant MongoDB object modeling for Node.js.
- Zod: TypeScript-first schema validation with static type inference.
- Structured for Scalability: A pre-defined folder structure to keep your code organized as it grows.
- Environment Variables: Easy configuration using
.envfiles.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
-
Clone the repository (or use it as a template on GitHub)
git clone https://github.com/yLost/backend-base-typescript.git cd backend-base-typescript -
Install dependencies
pnpm install
-
Set up environment variables Create a
.envfile in the root of the project. You can start by copying the example file. You will need to add your MongoDB connection string.cp example.env .env
Your
.envfile should contain variables like:MONGODB_URI=mongodb://localhost:27017/mydatabase PORT=3000
-
Development To run the server in development mode with auto-reloading:
pnpm dev
-
Production To build and run the server for production:
pnpm build pnpm start
The project is organized to separate concerns and encourage modularity.
/
├── dist/ # Compiled TypeScript files for production
├── src/
│ ├── database/ # MongoDB database connection setup
│ ├── decorators/ # Custom decorators
│ ├── errors/ # Custom error classes (e.g., BadRequest, MissingPermissions)
│ ├── interceptors/ # Middlewares and handlers
│ ├── models/ # Mongoose models
│ ├── modules/ # Feature modules containing services and controllers
│ ├── schemes/ # Zod schemas for data validation
│ └── app.ts # Application entry point and Fastify server setup
├── .env.example # Example environment variables
├── package.json # Project dependencies and scripts
└── tsconfig.json # TypeScript compiler options
- Node.js - JavaScript runtime
- TypeScript - Typed superset of JavaScript
- Fastify - Web framework for Node.js
- Mongoose - MongoDB object modeling tool
- Zod - Schema validation library
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request