For your first application lets assume that you are building an application for blogs and you want to create REST apis which can be consume by frontend/mobile application .
-
- API should create blogs into database.
- API should update blogs into database.
- API should delete blogs from database.
- API should find and fetch a blog using any existing blog id from database.
Prerequisite–
- mongodb is installed and server is running on default port.
- Node 6.9.0 is installed.
- IDE like vscode is available to you.
- Rest client like postman,curl for testing.
Installation
- git clone https://github.com/ratneshsinghparihar/nodedata-demo-sample.git
- cd nodedata-demo-sample/Demo-Sample
- npm install
Code changes
- Add a model blogmodel.ts inside models folder.
- Add a repository blogmodelrepository.ts inside repositories folder.
[gist]6beea0b1a959769ff529d53203a9bc6d[/gist]
- Model with @document tell the system to create a document inside blogs collection.
- repository with blog name will create all necessary rest end points.
Testing
- npm start
- Post a JSON {“name”: “testBlog”} to http://localhost:9999/data/blogs
- Hit the api to get data(http://localhost:9999/data/blogs)
- Hit the api with put method http://localhost:9999/data/blogs/{{blogId}} with body {“name”: “testBlog1”}
- Hit the api with delete method http://localhost:9999/data/blogs/{{blogId}}
Conclusion
Now you can see how creating rest apis is super easy with node-data. If you want to know more check out the our github
https://github.com/ratneshsinghparihar/Node-Data
Or visit our main page