A boilerplate for Node.js API. This boilerplate gives the minimal structure of application start with while bundling enough useful features so as to remove all those redundant tasks that can derail a project before it even really gets started. This boilerplate users Express with sequelize as ORM and MySQL/PostgreSQL as database.
NodeJs
NPM
MySQL/PostgreSQL
- Clone the repository with
git clone https://github.com/mangya/nodejs-rest-api.git <your_project_folder_name>
- Change directory to your project folder
cd <your_project_folder_name>
- Install the dependencies with
npm install
- Create database in MySQL/PostgreSQL.
- Update the your database name and credentials in the
.env
file. - Run the application with
npm start
(MySQL/PostgreSQL service should be up and running). - Access
http://localhost:3000
and you're ready to go!
.
├── app/
│ ├── controllers/ # Route controllers (controller layer)
│ ├── middlewares/ # Middlewares
│ ├── models/ # Express database models (data layer)
├── config/
├── database/
│ ├── migrations/ # Database migrations
├── helpers/ # Utility classes and functions
├── .env # API keys, passwords, and other sensitive information
├── routes/ # Route definitions
├── tests/ # Tests
├── index.js # Express application
└── package.json # NPM Dependencies and scripts
If you need to add more models to the project just create a new file in /models/
and use them in the controllers.
npx sequelize-cli model:generate --underscored --name <modelName> --attributes column1:string,column2:string,column3:string
npx sequelize-cli db:migrate
If you need to add more routes to the project just create a new file in /routes/
and add it in /routes/index.js
it will be loaded dynamically.
If you need to add more controllers to the project just create a new file in /controllers/
and use them in the routes.
npm test
You can set custom command for test at package.json
file inside scripts
property. You can also change timeout for each assertion with --timeout
parameter of mocha command.
If you need to add more test cases to the project just create a new file in /tests/
and run the command.
- nodemon — tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
- bcrypt — encryption library to hash a password
- body-parser — Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
- dotenv — module to load environment variables from a .env file
- express — web application framework for node
- helmet — Help secure Express apps by setting HTTP response headers.
- http-status — Utility to interact with HTTP status codes.
- passport — authentication middleware for Node.js.
- pg — PostgreSQL client for Node.js. (You can remove this if using MySQL as database)
- pg-hstore — A node package for serializing and deserializing JSON data to hstore format (You can remove this if using MySQL as database)
- mysql2 — MySQL client for Node.js. (You can remove this if using PostgreSQL as database)
- sequelize — Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server.
available in the repo
This boilerplate is open to suggestions and contributions, documentation contributions are also welcome! 😊