Skip to content

Commit 3156d37

Browse files
added required files
1 parent 279b6f8 commit 3156d37

27 files changed

+1487
-0
lines changed

Readme.md

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Node Express Backend Template 🚀
2+
3+
Welcome to the Node Express Backend Template! This template is designed to help you kickstart your Node.js and Express backend projects with a structured and scalable setup. It includes essential configurations, examples, and guidelines to streamline your development process. 🌟
4+
5+
## Who is this for? 🤔
6+
7+
This template is designed for:
8+
9+
- **Developers** who are looking to kickstart their Node.js and Express backend projects with a structured setup.
10+
- **Students** and **learners** wanting to understand how to structure a Node.js application with Prisma.
11+
- **Teams** needing a boilerplate for a scalable backend that can be customized and extended according to their project requirements.
12+
13+
## Table of Contents
14+
15+
| Section | Link |
16+
| ------------------ | ----------------------------------------- |
17+
| Overview | [Overview](#overview) |
18+
| Learning Resources | [Learning Resources](#learning-resources) |
19+
| Repo Structure | [Repo Structure](#repo-structure) |
20+
| Features | [Features](#features) |
21+
| Usage | [Usage](#usage) |
22+
| Tips for Beginners | [Tips for Beginners](#tips-for-beginners) |
23+
| Security Tips | [Security Tips](#security-tips) |
24+
| Contribution | [Contribution](#contribution) |
25+
| License | [License](#license) |
26+
| Getting Started | [Getting Started](#getting-started) |
27+
28+
---
29+
30+
## Overview 🌟
31+
32+
This repository provides a comprehensive template for backend projects, designed to streamline the setup process and ensure best practices are followed. It includes configurations for Node.js with Express.js, database integration using Prisma ORM, and essential middleware for authentication and validation. The template is CI/CD ready, making it easy to deploy and maintain. 🚀
33+
34+
### Tech Stack
35+
36+
[Node.js](https://nodejs.org/en/) | [Express](https://expressjs.com/) | [PostgreSQL](https://www.postgresql.org/) | [Prisma ORM](https://www.prisma.io/) | [JWT](https://jwt.io/) | [Bcrypt](https://www.npmjs.com/package/bcrypt) | [Joi](https://www.npmjs.com/package/joi)
37+
38+
### Prerequisites
39+
40+
Make sure you have the following dependencies installed on your machine:
41+
42+
- Node.js - [Download and install Node.js](https://nodejs.org/en/download/)
43+
- PostgreSQL - [Download and install PostgreSQL](https://www.postgresql.org/download/)
44+
45+
---
46+
47+
## Learning Resources 📚
48+
49+
If you're new to backend development, here are some resources to help you get started:
50+
51+
### Node.js and Express.js Basics
52+
53+
- **[Node.js Official Documentation](https://nodejs.org/en/docs/)**: Learn about Node.js, its features, and how to use it. 🟢
54+
- **[Express.js Official Documentation](https://expressjs.com/)**: Get familiar with Express.js and its powerful routing and middleware features. ⚡
55+
- **[MDN Web Docs: HTTP Basics](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview)**: Understand the basics of HTTP, the protocol used by web servers and clients. 🌐
56+
- **[Codecademy: Learn Node.js](https://www.codecademy.com/learn/learn-node-js)**: An interactive course to help you learn Node.js from scratch. 🎓
57+
58+
### Prisma ORM
59+
60+
- **[Prisma Official Documentation](https://www.prisma.io/docs/)**: Learn about Prisma and how to use it for database interactions. 🔍
61+
62+
### Git and GitHub 🛠️
63+
64+
- **[Git Documentation](https://git-scm.com/doc)**: Learn how to use Git for version control. 🔄
65+
- **[GitHub Guides](https://guides.github.com/)**: Explore guides on using GitHub to host your code and collaborate with others. 🤝
66+
67+
### Additional Learning Paths
68+
69+
- **[FreeCodeCamp: Backend Development](https://www.freecodecamp.org/learn/back-end-development-and-apis/)**: Free courses on backend development and APIs. 🆓
70+
- **[The Odin Project: Node.js](https://www.theodinproject.com/paths/full-stack-javascript/courses/nodejs)**: A comprehensive course on backend development with Node.js. 🛤️
71+
72+
---
73+
74+
## Repo Structure 📁
75+
76+
For more detailed information on each folder and file, please refer to the corresponding README pages linked below:
77+
78+
- **[Controllers](controllers/controller.md):** 🧠 Business logic functions that handle incoming requests and send responses to the client.
79+
80+
- **[Middleware](middlewares/middleware.md):** 🛡️ Middleware functions that handle specific tasks, such as authentication and validation, between the client request and the server response.
81+
82+
- **[Models](models/model.md):** 🗄️ Database ORM models that define the structure and relationships of the database tables.
83+
84+
- **[Prisma](prisma/prisma.md):** ⚙️ Prisma configuration and schema for database management.
85+
86+
- **[Services](services/service.md):** 🛠️ Business logic and authentication services.
87+
88+
- **[Utils](utils/util.md):** 🧰 General, reusable functionalities unrelated to API, such as file manipulation and encryption.
89+
90+
- **[Validators](validations/validation.md):** ✔️ Middleware functions for request object validation.
91+
92+
- **[Routes](routes/route.md):** 🛣️ Route definitions that determine the control flow of the API.
93+
94+
- **[Environment](environment.md):** 🌍 Configuration files for different environments.
95+
96+
- **[Index](index.js):** The main entry point for the application.
97+
98+
---
99+
100+
## Features 🌟
101+
102+
- **Structured Node.js and Express.js Setup**: A pre-configured setup for building robust and scalable backend applications. 🛠️
103+
- **Prisma ORM Integration**: Easily connect to SQL databases with Prisma ORM. 🗃️
104+
- **Basic Authentication**: Middleware for user authentication and authorization using JWT (JSON Web Tokens). 🔐
105+
- **Organized Code Structure**: Well-defined folders for controllers, models, routes, and more. 🗂️
106+
- **Validation Middleware**: Ensures incoming requests are validated. ✅
107+
- **Environment Management**: Manage environment-specific settings using `.env` files. 🌍
108+
- **Error Handling**: Centralized error handling to manage and log errors. 🚨
109+
- **Security Basics**: Basic security settings to protect your application. 🔒
110+
- **Detailed Documentation**: Instructions and explanations for easy navigation and learning. 📚
111+
112+
---
113+
114+
## Usage ⚙️
115+
116+
For detailed instructions on how to use this template, including examples, please refer to the [Usage Guide](documents/usage.md). Follow these steps to get started: 🚀
117+
118+
### Getting Started
119+
120+
1. Clone this repository to your local machine using the following command:
121+
122+
```bash
123+
git clone https://github.com/Velogan-Boy/node-headstart.git <project_name>
124+
```
125+
126+
2. Navigate to the project directory:
127+
128+
```bash
129+
cd <project_name>
130+
```
131+
132+
3. Install the dependencies:
133+
134+
```bash
135+
npm install
136+
```
137+
138+
4. Create a new PostgreSQL database for your project.
139+
5. Modify env files in the root directory of the project.
140+
141+
6. Modify the `schema.prisma` file in the `prisma` directory to match your database schema.
142+
143+
7. Run the following command to generate the Prisma client:
144+
145+
```bash
146+
npx prisma generate
147+
```
148+
149+
8. Run the following command to start the development server:
150+
151+
```bash
152+
npm run dev
153+
```
154+
155+
The server should now be running at http://localhost:5000.
156+
157+
> Note: Don't forget to uncomment the .env files in .gitignore file if you want to push your code to a remote repository.
158+
159+
---
160+
161+
## Tips for Beginners 💡
162+
163+
### Take it Step-by-Step 🐢
164+
165+
- **Start Small**: Focus on understanding one part of the codebase at a time. 🧩
166+
- **Use Documentation**: Refer to `README.md` files for each folder to understand their purpose and usage. 📄
167+
- **Experiment**: Modify small parts of the code to see how it affects the application. 🛠️
168+
169+
### Use the Resources 🌐
170+
171+
- **Official Docs**: Check Node.js, Express.js, and Prisma documentation for more details. 📚
172+
- **Community Help**: Seek assistance in forums like [Stack Overflow](https://stackoverflow.com/) or [Reddit’s LearnProgramming](https://www.reddit.com/r/learnprogramming/). 💬
173+
174+
### Don’t Rush ⏳
175+
176+
- **Take Breaks**: Learning to code can be intense. 🌟
177+
- **Focus on Understanding**: Ensure you understand each concept thoroughly. 🎯
178+
179+
---
180+
181+
## Security Tips 🔒
182+
183+
- **Keep Dependencies Updated**: Regularly update your project dependencies to patch any security vulnerabilities. 🔄
184+
- **Validate Inputs**: Always validate and sanitize user inputs to prevent injection attacks. 🛡️
185+
- **Use Environment Variables**: Store sensitive information in environment variables and never hard-code them into your application. 🌍
186+
- **Enable HTTPS**: Ensure your application uses HTTPS to encrypt data transmitted between the client and server. 🔐
187+
188+
---
189+
190+
## Customizing the Template 🛠️
191+
192+
To tailor this template to your specific needs, follow these steps:
193+
194+
### 1. **Update Environment Variables**
195+
196+
- **Edit `.env` File**: Configure environment variables in the `.env` file located in the root directory. You can set different configurations for development, staging, and production by creating respective `.env.development`, `.env.staging`, and `.env.production` files. Refer to the [Environment Configuration](environment.md) documentation for details.
197+
198+
### 2. **Modify Database Configuration**
199+
200+
- **Update Prisma Schema**: Customize your database schema by editing `schema.prisma` in the `prisma` folder. Define your data models and relationships here. For more information, see [Prisma Configuration](/prisma/prisma.md).
201+
- **Adjust Database Credentials**: Change the database connection settings in your `.env` file to match your database setup.
202+
203+
### 3. **Customize Middleware**
204+
205+
- **Edit Middleware**: Modify or add new middleware functions in the `middlewares` folder. Update the `middleware.md` file with any new middleware functionalities you introduce. Review existing middleware in `exampleMiddleware.js` for guidance.
206+
207+
### 4. **Update Controllers**
208+
209+
- **Adjust Business Logic**: Customize controller functions in the `controllers` folder. Update `controller.md` with explanations of new controller functionalities. Refer to `exampleControllers.js` for examples on how to structure your controllers.
210+
211+
### 5. **Revise Routes**
212+
213+
- **Modify Routes**: Update route definitions in the `routes` folder. Adjust `v1/exampleRouter.js` or add new route files as needed. Document any new routes or changes in the `route.md` file.
214+
215+
### 6. **Adapt Models**
216+
217+
- **Edit ORM Models**: Modify the database models in the `models` folder. Update `model.md` with details on the data models you use. Refer to `exampleModel.js` for examples of model structure.
218+
219+
### 7. **Adjust Utilities**
220+
221+
- **Update Utility Functions**: Modify or add utility functions in the `utils` folder. Update `util.md` with new utilities or changes. Refer to `hashPassword.js` for examples.
222+
223+
### 8. **Configure Validations**
224+
225+
- **Modify Validation Rules**: Update validation schemas and rules in the `validations` folder. Document any changes in `validation.md` and see `exampleValidation.js` for examples of validation rules.
226+
227+
### 9. **Set Up Additional Services**
228+
229+
- **Customize Services**: Update or add new services in the `services` folder. Adjust `service.md` to include new service functionalities. Refer to `jwtAuth.js` for examples.
230+
231+
### 10. **Adjust Documentation**
232+
233+
- **Update Documentation**: Ensure all changes are reflected in the documentation. Update or add new `.md` files as needed to keep documentation accurate and helpful.
234+
235+
By following these steps, you can effectively customize the template to fit your project requirements. If you need further customization, refer to the detailed documentation provided in each folder.
236+
237+
---
238+
239+
## Creating a Good README
240+
241+
A well-crafted README is essential for any project. It serves as the first point of contact for users and contributors, providing them with a clear understanding of the project’s purpose, setup instructions, and contribution guidelines.For suggestions on how to create a good README, please refer to the [Good README Guide](good-readme.md).
242+
243+
## Contribution
244+
245+
Contributions to enhance the structure or add new features to this boilerplate are welcome. Here are some ways you can contribute:
246+
247+
- **Reporting Bugs**: If you find a bug, please report it by opening an issue.
248+
- **Feature Requests**: If you have an idea for a new feature, feel free to suggest it.
249+
- **Pull Requests**: If you want to contribute code, fork the repository and create a pull request with your changes.
250+
251+
Please check out our [Contributing Guide](contributing.md) for more details.
252+
253+
## License
254+
255+
This project is licensed under the MIT License. This means you are free to use, modify, and distribute the software, provided that you include the original copyright and license notice in any copies of the software. For more details, see the [LICENSE](LICENSE) file.
256+
257+
---
258+
259+
## Getting Started
260+
261+
Follow these steps to start using this template:
262+
263+
1. Clone the repository to your local machine.
264+
2. Install the required dependencies using `npm install` or `yarn install`.
265+
3. Set the configuration the database settings and environment variables in the `config` folder.
266+
4. Start the application using `npm start` or `yarn start`.
267+
5. Use the API endpoints defined in the `routes` folder to interact with the application.
268+
269+
For more detailed setup instructions, see the [Setup Guide](setup.md).
270+
271+
---
272+
273+
## [❤️ Sponsor Me](https://github.com/sponsors/MettaSurendhar)
274+
275+
**If you appreciate my work and would like to support me, consider sponsoring me!** Your support helps me continue to create and maintain open-source projects.
276+
277+
- **Why Sponsor?**: Sponsoring helps sustain the development of open-source projects and allows creators to dedicate more time to their work.
278+
279+
## [🍴 Fork this Repository](https://github.com/MettaSurendhar/express-api-boilerplate/fork)
280+
281+
**Fork this repository to customize or contribute to the project!** Forking allows you to make changes independently and contribute back through pull requests.
282+
283+
- **Why Fork?**: Forking lets you create your own copy of the project for personal modifications or development, and it enables collaboration through pull requests.
284+
285+
## [🌟 Star this Repository](https://github.com/MettaSurendhar/express-api-boilerplate)
286+
287+
**Show your support by starring this repository!** Starring helps us gauge interest and lets others know that this project is valuable.
288+
289+
- **Why Star?**: Starring helps indicate the project's usefulness and can attract more contributors.
290+
291+
## [📄 Use This Template](https://github.com/new?template_name=node-express-backend-template-for-intermediate&template_owner=MettaSurendhar)
292+
293+
This repository is designed as a **template** for creating Express.js APIs. You can use this template to kickstart your own projects by clicking the **Use this template** button on GitHub.

controllers/controller.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Controllers 📋
2+
3+
## Controllers - Business Logic Functions 🧠
4+
5+
### Overview 🌐
6+
7+
This folder contains the business logic functions that handle incoming requests and send responses to the client. These functions form the core of your application’s API, deciding how to process data and interact with other parts of the system.
8+
9+
### Structure 🗂️
10+
11+
- **Each controller file** corresponds to a specific API endpoint or resource, such as users, products, or orders.
12+
- **Functions within each controller** are responsible for:
13+
- **Processing Requests:** Receiving data from client requests and extracting parameters or body content.
14+
- **Executing Business Logic:** Performing operations like querying a database, manipulating data, or interacting with external APIs.
15+
- **Sending Responses:** Returning the results back to the client in a structured format (e.g., JSON), along with appropriate HTTP status codes.
16+
17+
### Purpose 🎯
18+
19+
The purpose of these controllers is to abstract and organize the logic required to manage the different resources of your application, keeping your code modular and maintainable.
20+
21+
#### Example 🚀
22+
23+
📦 **`exampleController.js`:** Manages example-related requests such as retrieving, creating, updating, or deleting example records.
24+
25+
- **`getAllExamples:`** Handles GET requests to fetch all example records. Typically used to display a list of all items.
26+
- **`getExampleById:`** Handles GET requests for a specific example record by ID. Useful for viewing details of a single item.
27+
- **`createExample:`** Handles POST requests to add a new example record. Used when a client submits a form to create a new item.
28+
- **`updateExample:`** Handles PUT requests to modify an existing example record by ID. Allows clients to update details of an item.
29+
- **`deleteExample:`** Handles DELETE requests to remove an example record by ID. Used when an item needs to be permanently deleted from the database.
30+
31+
Feel free to explore and modify the controller files to suit your application’s needs! 🔍
32+
33+
### Usage 🚀
34+
35+
These functions can be tied to specific routes in your application, allowing them to handle incoming API requests. For instance, you might set up the following routes:
36+
37+
```javascript
38+
const express = require('express');
39+
const router = express.Router();
40+
const exampleController = require('./controllers/exampleController');
41+
42+
router.get('/examples', exampleController.getAllExamples);
43+
router.get('/examples/:id', exampleController.getExampleById);
44+
router.post('/examples', exampleController.createExample);
45+
router.put('/examples/:id', exampleController.updateExample);
46+
router.delete('/examples/:id', exampleController.deleteExample);
47+
48+
module.exports = router;
49+
```
50+
51+
### How It Works 🛠️
52+
53+
- **Define Routes:** The routes are defined in the routes/exampleRoutes.js file. Each route corresponds to an API endpoint that your client can interact with.
54+
55+
- **Map Routes to Controller Functions:** Each route is linked to a specific function in the exampleController.js. For instance, a GET request to /api/examples will invoke the getAllExamples function to retrieve all example records.
56+
57+
- **Middleware Integration:** You can easily integrate these routes with your main application by importing the router in your app.js or index.js file:
58+
59+
#### Testing the API: With this setup, you can now test your API using tools like Postman or curl. For example:
60+
61+
- GET /api/examples: Retrieve all example records.
62+
- GET /api/examples/1: Retrieve the example record with ID 1.
63+
- POST /api/examples: Create a new example record.
64+
- PUT /api/examples/1: Update the example record with ID 1.
65+
- DELETE /api/examples/1: Delete the example record with ID 1
66+
67+
### Customization 🛠️
68+
69+
- **Adjust Routes:** Feel free to modify the routes or controller logic to fit the specific needs of your application.
70+
71+
- **Add Middleware:** You can integrate authentication, validation, or other middleware to enhance your API's functionality and security.

0 commit comments

Comments
 (0)