From the course: Angular: API Communication and Authentication

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

POST route for the API

POST route for the API - Angular Tutorial

From the course: Angular: API Communication and Authentication

Start my 1-month free trial

POST route for the API

- [Instructor] Now that we have our get route for retrieving our contacts, we need to setup a post route that we can use to add new contacts. When performing a post request data is sent in the body of the request. We can go ahead and use the body parser middleware in order to parse incoming requests. We need our API to parse JSON, so we use the bodyParse.json middleware function, which will make the data available in the body of the request in our handlers. I've made a small change. Instead of just having our route at the forward slash contacts, I've prepended the forward slash API route. It's a standard convention to follow when developing your own API. Similar to our get request, we listen for post requests at the forward slash API slash contacts route. We can retrieve the user being added in the body of the request. This is thanks to our body parser middleware function. Next, we obtain a reference to the contacts collection, and then we make call to insert the posted user using the…

Contents