Structure my Application in Express.js
Last Updated :
03 Oct, 2025
In Express, split the app into small parts for clarity and speed. Use routes for endpoints, controllers for request handling, middleware for auth and logs, services for business rules, and models for database work. This structure keeps the code readable, testable, and scalable.
First, set up Express.js in your project.
Folder Structure Overview
Here’s an advanced and effective folder structure for your Express.js application
Folder structure
The folder structure lists all folders and files in the current project directory, including config, controllers, models, databases, routes, views, tests, etc. Below are detailed explanations of each folder and file in the structure.
1. config/ Folder
This folder holds configuration files related to the database, environment, and other settings.
- dbConfig.js: Contains the configuration for connecting to the database, like the host, username, and password.
module.exports = {
dbUrl: process.env.DB_URL || 'mongodb://localhost:27017/mydb',
};- Separation of Configurations: Keeps all environment configurations in one place for easier management.
- Environment Flexibility: Allows easy switching of configurations for different environments (development, production).
- Database Configuration: Centralizes database connection settings for better maintainability.
2. controllers/ Folder
Controllers contain the logic for each route, handling requests, processing data, and sending responses. This keeps routes clean and focused on HTTP handling.
const User = require('../models/userModel');
exports.getAllUsers = (req, res) => {
User.find({}, (err, users) => {
if (err) return res.status(500).send('Error retrieving users');
res.status(200).json(users);
});
};- Business Logic Separation: Keeps routes focused on request handling, while the controller manages business logic.
- Database Interaction: Interacts with the models to fetch data or perform actions.
- Error Handling: Ensures that errors in data retrieval are handled gracefully.
3. databases/ Folder
This folder contains database connection logic.
- Mysql.js: Manages the MySQL database connection.
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'mydb'
});
connection.connect();
module.exports = connection;- Database Connection: Encapsulates the logic for establishing database connections.
- Reusability: This file can be imported in different parts of the app to interact with the database.
- Environment Setup: Makes it easy to configure different databases for different environments (development, production).
4. env/ Folder
Contains environment variables and configurations like API keys, JWT secrets, etc.
- .env: Stores environment-specific variables like the database URL or JWT token.
DB_URL=mongodb://localhost:27017/mydb
JWT_SECRET=mysecretkey
- Secure Storage: Keeps sensitive data like database credentials and secrets out of the codebase.
- Environment Configuration: Allows you to configure different settings for development, staging, and production environments.
5. middlewares folder
Middleware functions are executed during the request-response cycle. They can perform tasks like authentication, logging, or validation before the request reaches the route handler.
module.exports = (req, res, next) => {
const token = req.headers['authorization'];
if (!token) return res.status(403).send('No token provided');
// Check token validity here
next();
};- Authentication: Middleware is used to secure routes by verifying tokens.
- Reusable Logic: Can be applied to multiple routes to handle common tasks like authentication or logging.
- Flow Control: Uses next() to pass control to the next middleware or route handler.
6. models/ Folder
Models define the structure of your data and interact with the database.
- userModel.js: Defines the structure of user data, such as name and email.
const log = (message) => {
console.log([LOG]: ${ message });
};
module.exports = log;- Schema Definition: Defines the structure of the data that will be stored in the database.
- Data Validation: Ensures that required fields are present before data is saved.
- Database Interaction: Provides a model to interact with MongoDB or other databases.
7. public/ Folder
This folder contains static files like HTML, images, and CSS that are served directly to the client.
- index.html: The main HTML page.
- Static Assets: Stores files that are directly accessible to the client without any server-side processing.
- Client-Side Resources: Contains HTML, CSS, and image files for the frontend.
8. routes/ Folder
Routes manage HTTP requests (GET, POST, PUT, DELETE) and map them to controller functions.
- userRoutes.js: Defines the routes for user operations like fetching all users.
const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');
router.get('/users', userController.getAllUsers);
module.exports = router;- Route Management: Organizes routes based on features or entities (e.g., userRoutes).
- Cleaner Code: Separates the routing logic from the controller logic, keeping files smaller and easier to manage.
9. services/ Folder
Contains business logic functions that often interact with models and perform more complex operations.
- authService.js: Handles user authentication logic.
- productService.js: Contains logic related to products, like fetching product data.
10. tests/ Folder
Contains unit and integration tests to ensure that the application works as expected.
- Integration.js: Integration tests for testing multiple components together.
- unit.js: Unit tests for individual components or functions.
11. utils/ Folder
Contains helper functions like logging or error handling that can be reused throughout the app.
- logger.js: A simple logging utility.
module.exports = (message) => {
console.log(`[LOG]: ${message}`);
};- Reusable Functions: Stores commonly used functions to avoid code duplication.
- Utility Focus: Contains utility functions that enhance code readability and maintainability.
12. views/ Folder
If using a templating engine like EJS or Pug, this folder contains the views (templates) for rendering dynamic HTML on the server-side.
- index.ejs: The main template used to render dynamic HTML content.
- Dynamic Rendering: Allows server-side rendering of dynamic content before sending it to the client.
- Template Reusability: Templates help in reusing HTML code and keeping it DRY (Don’t Repeat Yourself).
13. server.js File
The entry point of the application. It starts the Express server and listens for incoming requests.
const app = require('./app');
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});- Server Setup: Initializes the server and listens for requests.
- App Initialization: Loads the Express app from app.js and starts the server.
14. app.js File
Contains the core logic for your Express application, including routing, middleware, and settings.
15. package.json File
Manages project dependencies, scripts, and metadata for your application.
16. README.md File
Contains project documentation, including setup instructions and project details.
Explore
Node.js Tutorial
3 min read
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools