A boilerplate for building scalable and maintainable REST APIs using Node.js, Express.js, Sequelize.js, Yup and JWT. This project provides a strong foundation with an organized directory structure and pre-configured tools to streamline your development process.
- Node.js: A JavaScript runtime built on Chrome's V8 JavaScript engine.
- Express.js: A minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
- Sequelize.js: A promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server.
- Yup Validations: Schema-based validation library for runtime value validation.
- JWT Authentication: JSON Web Token (JWT) authentication for secure user sessions.
- One-Device Login: Ensures that users can only be logged in from one device at a time, enhancing security.
- Readable Error Messages: Provides user-friendly error messages to improve the user experience.
- Centralized Error and Response Handler: Centralized system to handle errors and responses, making the application more maintainable and consistent.
-
Clone the repository:
git clone https://github.com/princemeghani/nodejs-express-sequelize-boilerplate.git
-
Enter into the directory
cd nodejs-express-sequelize-boilerplate -
Install dependencies:
npm install # or yarn -
Configure environment variables: Rename
.env.sampleto.envfile in the root directory and add the necessary environment variables.Variable Description Example NODE_ENVEnvironment (development/production) developmentSERVER_PORTPort number for the server 4000DB_DIALECTmysql, postgresql, among others mysqlDB_HOSTDatabase host localhostDB_USERDatabase user rootDB_PASSDatabase password passwordDB_NAMEDatabase name database_nameSERVER_JWT_SECRETSecret key for JWT your_jwt_secretSERVER_JWT_TIMEOUTJWT time expiration 1d -
Create database:
npx sequelize db:create # or yarn sequelize db:create -
Run database migrations:
npx sequelize db:migrate # or yarn sequelize db:migrate -
Start the application:
npm start # or yarn start
.
├── src
│ ├── config # Configuration files
│ ├── controllers # Route controllers
│ ├── middlewares # Custom middlewares
│ ├── models # Sequelize models
│ ├── routes # Express routes
│ ├── utils # Utility functions
│ └── app.js # Express app initialization
├── .env # Environment variables
├── .gitignore # Git ignore rules
├── package.json # NPM dependencies and scripts
└── README.md # Project documentation
# Creates the database
npx sequelize db:create
# or
yarn sequelize db:create
# Drops the database
npx sequelize db:drop
# or
yarn sequelize db:drop
# Load migrations
npx sequelize db:migrate
# or
yarn sequelize db:migrate
# Undo migrations
npx sequelize db:migrate:undo:all
# or
yarn sequelize db:migrate:undo:all
# Load seeders
npx sequelize db:seed:all
# or
yarn sequelize db:seed:allThe one-device login feature ensures that each user can only be logged in from one device at a time. If a user logs in from a new device, their previous session will be invalidated. This is implemented using JWT tokens and a session management system in the database.
Yup is used for schema-based validation to ensure that incoming data meets the required format and constraints. This helps in maintaining data integrity and providing clear validation errors to the users.
JWT authentication is used to secure user sessions. Users receive a JWT upon successful login, which must be included in subsequent requests to authenticate the user. This enhances security and provides a scalable way to manage user sessions.
Readable error messages are provided to users in a friendly and understandable format. This helps in identifying and resolving issues quickly.
All errors and responses are handled centrally to maintain consistency and improve maintainability. This is achieved through custom middleware that processes all outgoing responses and errors.
We welcome contributions! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.