A comprehensive Node.js template designed with best coding practices and project management recommendations. Feel free to customize it according to your project needs!
This folder contains all the actual source code for the project, excluding tests (consider creating a separate test/ folder).
Let's explore the src/ folder structure:
Contains all configurations and setup files for libraries and modules.
- Example:
server-config.js- Sets up dotenv for cleaner environment variable usage - Example: Logging library configurations for meaningful application logs
Registers routes with their corresponding middleware and controllers.
Intercepts incoming requests for validation, authentication, and other preprocessing tasks.
Acts as the final middleware layer before business logic execution.
- Receives incoming requests and data
- Passes data to the business layer
- Structures API responses and sends output
Contains all database interaction logic.
- Raw SQL queries
- ORM queries
- Database-specific operations
Houses business logic and interacts with repositories for database operations.
Contains helper methods, error classes, and utility functions.
- Download this template from GitHub
- Open it in your favorite text editor
Create a .env file in the root directory and add the following:
npm install
PORT=<port number of your choice>
Example:
PORT=3000
Inside the src/config/ folder, create a file named config.json with the following content:
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
Update the following fields in the development section:
username- Your database usernamepassword- Your database passworddialect- Your database type (mysql, postgres, sqlite, etc.)
For test or production environments:
- Replace all database credentials accordingly
- Important: Replace the
hostvalue with your hosted database URL
npm run dev