API with Mongo Express and Node (the sequel)
Dave Okwii dev blog

API with Mongo Express and Node (the sequel)

I was creating the waiter who will connect the back-end of the application to the front-end. The link to the exciting prequel is below.

The Model...

The model refers to the way the database JSON file will look like based on the user specification.

No alt text provided for this image

First, we require mongoose and describe the schema as an object with the 'fields' title and post. Both fields will be required by the database to be filled and the API will make sure of it.

The Routes...

No alt text provided for this image

In many ways (pun intended), the routes provide the basis for the API as it shows the methods to be used to create, retrieve, update and delete the data. Through the use of the HTTP request methods, the CRUD functionalities are achieved. The HTTP request methods include GET, PATCH or PUT and POST. The actions are applied at specific endpoints. Firstly, we should create an instance of the express router and assigning it to the constant router so as to use it as the middle-ware. The router object is chained together with the API endpoint.

router.HTTP Method(path, handler function)

The first GET HTTP method is used return a JSON array of the blog posts found in the database we created. Next we get request to the path of posts with the specific id so that to go to specific blog posts. If the blog post is not found, am error message is displayed.

No alt text provided for this image


The Post request creates a new instance of a blog post based on the structure of the model we had created. The database automatically assigns an ID to the new blog post. Successful save will be seen using status 200. The Patch requests takes the path of the specific post and the handler function is used to change the payload. In case of failure, the response 422 is returned to the user, otherwise a JSON response of successful updating.

No alt text provided for this image

The Delete requests firsts checks to see if the specific post exists and if it exists, proceeds to delete it. When you restart your server and go to https://localhost:5000/api/blogposts you should see an empty array. If you use apps like postman, mongodb compass and curl you can test to see if your endpoints are working.

That's all folks! I hope it can be clearly understood. Feel free to correct, add or comment on both posts.



要查看或添加评论,请登录

Paul C.的更多文章

  • Component Interaction In Angular

    Component Interaction In Angular

    Component interaction refers to communication between two or more components created in an Angular application. This…

  • View Encapsulation

    View Encapsulation

    Encapsulation is the concept of bundling similar blocks of code together as one unit. In this article, I talk about…

    2 条评论
  • Angular Lifecycle methods

    Angular Lifecycle methods

    Lifecycle hooks are methods called from the creation of a component or a directive to its destruction. For directives…

  • NG Module metadata

    NG Module metadata

    Ng module metadata refers to a single object within an angular module whose properties are used in an angular…

  • Basic MongoDB Commands

    Basic MongoDB Commands

    Starting MongoDB mongo --host localhost:27017 2. Log Into mongoDB.

  • API with Mongo, Express and Node (part 1)

    API with Mongo, Express and Node (part 1)

    APIs, Application Programming Interfaces, are essential for any web and mobile applications since they serve as…

  • My first boot-camp month!

    My first boot-camp month!

    How a month goes by so fast! Such an interesting month. I could talk about the data structures, I could talk about the…

    4 条评论

社区洞察

其他会员也浏览了