Backend-Template-TypeScript

Backend Template TypeScript

This repository provides a production-grade backend template for Node.js applications using Express.js and TypeScript, with Mongoose for MongoDB integration. It includes a foundational structure, environment configuration, and a basic User model to jump start your backend development.

Features

Folder Structure

Backend Template TypeScript/
├── node_modules/             # Dependencies
├── public/                   # Static assets
├── src/                      # Main source code
│   ├── index.ts              # Entry point
│   ├── app.ts                # Express app setup
│   ├── constant.ts           # Constants file
│   ├── controllers/          # Controller files
│   │   └── user.controller.ts
│   ├── db/                   # Database connection setup
│   │   └── database.ts
│   ├── middlewares/          # Middleware functions
│   │   ├── auth.middleware.ts
│   │   └── multer.middleware.ts
│   ├── models/               # Data models
│   │   └── user.model.ts
│   ├── routes/               # Route handlers
│   │   └── user.routes.ts
│   └── utils/                # Utility functions
│       ├── ApiError.ts
│       ├── ApiResponse.ts
│       ├── asyncHandler.ts
│       └── cloudinary.ts
├── .env                      # Environment variables
├── .env.example              # Example .env file
├── .eslintrc.json            # ESLint configuration
├── .gitignore                # Git ignore file
├── package-lock.json         # Locked dependencies
├── package.json              # Project dependencies and scripts
├── Readme.md                 # Project documentation
└── tsconfig.json             # TypeScript configuration

Getting Started

Prerequisites

Environment Variables

Create a .env file in the root directory and add the following:

# Example .env file

PORT=4000
MONGODB_URL=YourMongoDBURL
CORS_ORIGIN=*

ACCESS_TOKEN_SECRET=Darshan
ACCESS_TOKEN_EXPIRY=1d
REFRESH_TOKEN_SECRET=Dpvasani
REFRESH_TOKEN_EXPIRY=10d

CLOUDINARY_CLOUD_NAME=YourCloudName
CLOUDINARY_API_KEY=YourApiKey
CLOUDINARY_API_SECRET=YourApiSecret

Installation

  1. Clone the repository:
git clone https://github.com/dpvasani/Backend-Template-TypeScript
cd Backend-Template-TypeScript
  1. Install dependencies:
npm install
  1. Set up your MongoDB connection string and other environment variables in the .env file.

Start the Server

To run the development server:

npm run dev

This will start the development server.

Contributing

Feel free to open issues or create pull requests if you have suggestions for improvements!

License

This project is licensed under the MIT License.