How To Add Routes and Models To Node Rest API

How To Add Routes and Models To Node Rest API

At the conclusion of our last tutorial which looks at working with Models in Mongoose, we now have a REST api which allows us to create new users and authenticate users. In a prior tutorial we also built out a restful resource for Games. In other words, we were able to create, read, update, and delete Games using the Node powered api. Now, we’ll once again go over the process of how routes files and model files work together in an express app.

Let’s start at index.js and see what we have so far.

Notice the second arguments of users and auth. Those correspond to the /routes/users.js and /routes/auth.js routes files. Now we are going to add a way to allow users to add a new Platform for which will allow users to set up various game platforms such as Nintendo Switch, Xbox One, or Sony Playstation. Let’s see how to set this up.

1. Set Up index.js

First we can configure the index.js file for a new route path / routes file association.

2. Set up platforms.js Routes file

Now we need to add a new file in our routes directory like so. We’ll call it platforms.js.
add new routes file to nodejs app

3. Set up platform.js Model file

We also need a model for the Platform, we can add a platform.js model file to the /models directory.
add model to nodejs app

Building Out A Model File

First up, we’ll require Joi and Mongoose, and begin building a new schema. The Platform will be a string that is required with a minimum length of 5 and a maximum length of 50.

Then, using our Schema, we can easily compile a model from it like so.

Now, we can build a very simple validation function which specifies a 3 character minimum for the Platform.

Lastly, we’ll export the platformSchema, the Platform model, and the validatePlatformfunction. These lines are highlighted in the context of our full platform.js file here.

Building Out A Routes File

In this case, we’re building the routes file last because we are actually depending on code that was written in the model file for use in our routes file. In this file we’ll need access to the Platform model, the validate function, mongoose, express, and the express router. We set all of that up first.

Configure POST Request To Add a Platform

We don’t have any Platforms in our system yet, so lets first build a way to add some using our REST api. Here we validate a request, and if there are no errors, persist the platform to Mongodb.

Important! Don’t forget that module.exports = router; code, otherwise you will suffer some error such as throw new TypeError(‘Router.use() requires a middleware function but got a ‘ + gettype(fn))

Ok! I think we can try adding a new Platform to the system by sending a POST request to our new endpoint of http://localhost:4000/api/platforms. Here we will add your favorite gaming platform and mine, the Nintendo Switch. Boot up the server by running nodemon index.js and we can test it out.
Nintendo Switch Platform

In the background we can add several more platforms as well so that we have a few we can work with.

Configure a GET request to view all Platforms

Wouldn’t you like to know the other platforms we added to the system? Of course you would. This is why we will add a GET request endpoint to our api so we can now view all platforms currently in MongoDB. Check it out.

Simple! Now, make a GET request in Postman.
GET request to node api using postman

We get this pretty sweet JSON collection of Video Game Platforms.

You may also make a simple GET request using a web browser to the http://localhost:4000/api/platforms endpoint. Just paste in the endpoint and hit enter! Why? Because any time you are trying to view something in a web browser, the browser is simply making a GET request to the given URL provided. So here goes.
google chrome get request node api

Configure a PUT request endpoint to modify Platforms

If you want to be able to send a PUT request to the API in order to make an update to a resource, here is how we can do it. We’ll make use of the handy findByIdAndUpdate method.

Now, we want to change the name of the Playstation platform to Playstation PRO. First, we determine the id of the Platform we want to update. In this case, it is 5b3a5ea730246041d8fa951e. Therefore, we send a PUT request using postman to http://localhost:4000/api/platforms/5b3a5ea730246041d8fa951e while also setting a JSON object in the body of the request with the updated name value. Bazinga!
PUT request to node api

Configure a DELETE request endpoint to delete Platforms

Now we want to allow for the ability to delete a Platform from the system. So you’re probably seeing a theme here. For each resource, it is pretty easy to set up the CRUD functions. Here is that DELETE option we want for our Platforms resource. Here is our code to handle the DELETE request which completes all of the crud operations for this resource. Note the handy use of findByIdAndRemove.

So what should we delete? Let’s delete the Mobile IOS platform. First, we determine it’s id of 5b3a5eb730246041d8fa951f. We’ll need that to send a correct DELETE request with Postman. Here we go.
node rest delete request

How To Add Routes and Models To Node Rest API Summary

To be fair, a lot of what we did here is almost the same as what we covered in the express rest api tutorial. In that first one we dealt with Games. Here we are working with Platforms those games can be played on. The take away however is getting our thinking about the process of imagining an endpoint we would like to have, creating a the app.use() statement in the index.js file, setting up a dedicated Model file, and finally, setting up the dedicated routes file. With this we understand how each of the files export and require and talk to each other to make the api work.