A ready-to-use boilerplate for REST API Development with Node.js, Express, and MongoDB
This is a basic API skeleton written in JavaScript ES2015. Very useful to building a RESTful web APIs for your front-end platforms like Android, iOS or JavaScript frameworks (Angular, Reactjs, etc).
This project will run on NodeJs using MongoDB as database. I had tried to maintain the code structure easy as any beginner can also adopt the flow and start building an API. Project is open for suggestions, Bug reports and pull requests.
This advert was placed by GitAds
I am looking for a good job or work contract. You can contact me directly on my email (maitraysuthar@gmail.com) or you can download my CV from my personal website. Looking forward. Thanks π
If you consider my project as helpful stuff, You can appreciate me or my hard work and time spent to create this helpful structure with buying a coffee for me. I would be very thankful if you buy me a coffee, please buy me a coffee π.
- Basic Authentication (Register/Login with hashed password)
- Account confirmation with 4 (Changeable) digit OTP.
- Email helper ready just import and use.
- JWT Tokens, make requests with a token after login with
Authorization
header with valueBearer yourToken
whereyourToken
will be returned in Login response. - Pre-defined response structures with proper status codes.
- Included CORS.
- Book example with CRUD operations.
- Validations added.
- Included API collection for Postman.
- Light-weight project.
- Test cases with Mocha and Chai.
- Code coverage with Istanbuljs (nyc).
- Included CI (Continuous Integration) with Travis CI.
- Linting with Eslint.
- Node.js 8+
- MongoDB 3.6+ (Recommended 4+)
- Clone the project from github. Change "myproject" to your project name.
git clone https://github.com/maitraysuthar/rest-api-nodejs-mongodb.git ./myproject
- Download repository
- Uncompress to your desired directory
cd myproject
npm install
- You will find a file named
.env.example
on root directory of project. - Create a new file by copying and pasting the file and then renaming it to just
.env
cp .env.example .env
- The file
.env
is already ignored, so you never commit your credentials. - Change the values of the file to your environment. Helpful comments added to
.env.example
file to understand the constants.
.
βββ app.js
βββ package.json
βββ bin
β βββ www
βββ controllers
β βββ AuthController.js
β βββ BookController.js
βββ models
β βββ BookModel.js
β βββ UserModel.js
βββ routes
β βββ api.js
β βββ auth.js
β βββ book.js
βββ middlewares
β βββ jwt.js
βββ helpers
β βββ apiResponse.js
β βββ constants.js
β βββ mailer.js
β βββ utility.js
βββ test
β βββ testConfig.js
β βββ auth.js
β βββ book.js
βββ public
βββ index.html
βββ stylesheets
βββ style.css
npm run dev
You will know server is running by checking the output of the command npm run dev
Connected to mongodb:YOUR_DB_CONNECTION_STRING
App is running ...
Press CTRL + C to stop the process.
Note: YOUR_DB_CONNECTION_STRING
will be your MongoDB connection string.
If you need to add more models to the project just create a new file in /models/
and use them in the controllers.
If you need to add more routes to the project just create a new file in /routes/
and add it in /routes/api.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 /test/
and run the command.
npm run lint
You can set custom rules for eslint in .eslintrc.json
file, Added at project root.
Every project needs improvements, Feel free to report any bugs or improvements. Pull requests are always welcome.
This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.