Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing REST APIs Chapter 4

Now create a new query in Postman with the following parameters. We will create a GET


request with the URL http://localhost:3000/users/:user_id where user_id is the


id of any user that you have created in your database. With this setting, we should be able


to view something like this:


The query should return the details of the user with the given ID in the URL.


Adding a POST endpoint in the users controller

Let's look at an example. Let's create an API that will use the MongoDB insert()


command to save user resources in the database. In the users controller, add a new


endpoint:


// add a new user
app.post('/users', (req, res) => {
const user = new User({
name: req.body.name,
email: req.body.email
})

user.save(function (error, user) {
Free download pdf