Boilerplate for getting started with REST-API development projects using Node, Express & MongoDB.
- Install Express and Setup App
- Setup Item Routes
- Setup DB and Item Model
- Add Data Validation
- Add Automated Tests (Mocha, Chai, Chai-http)
- Add Authentication and API Authorization using JWT
- Add handlers to take care of business logic currently in routes
Feel free to fork/clone.
- Database: MongoDB
- ODM: Mongoose
- Runtime: NODE
- Framework: Express
- Testing: Mocha, Chai, Chai-http
- Clone this repo into your local working dir
- Install all dependencies. Run
npm install
- Create a config folder in your root directory and a file under config named keys.js. Inside keys.js, export an object with two keys "mongoDevURI" and "mongoTestURI"; values should be the endpoint to your mongoDB instances (for both development and testing), either on MongoDB Atlas or locally". For example
module.exports = {
mongoDevURI: encodeURI('mongodb+srv://username:password@*******************')
}
Note that this is only for development purposes. In production, this value should be an environment variable.
- Run
npm start
At this point, the API endpoints should work. Simply use postman to test these endpoints.
@route GET api/items
@desc GET items
http://localhost:{port_number}/api/items
@route POST api/items
@desc Add new item. Pass new item name in request body
http://localhost:{port_number}/api/items
@route PUT api/items/:id
@desc Update existing item. Pass ID of item to update as a request parameter
http://localhost:{port_number}/api/items/{item_id}
@route DELETE api/items/:id
@desc Delete existing item. Pass ID of item to delete as a request parameter
http://localhost:{port_number}/api/items/{item_id}
- To run the automated tests, Run
npm test