In part 1 of building our todo app we set up our Mongo database, express, ejs, and our body parser. It is ready and listening.

Adding Seed Data

Now it’s time to add seed data. Seed data is initial data when you first create the database it’s empty and you seed it with its initial data. Usually it’s for your application but sometimes for sample user data. Now to do this I created a controllers folder. Inside the folder I created a file called setupController.

In setupController I require a land export the todo module we created in part 1. Then I created some fake data with JSON generator to test large data. We are just creating JSON objects that work throughout the MEAN stack.

With our new dummy data we use the .create method to create these objects and run it.

From there I just ran nodemon in the command terminal and checked to see if the data ran in the local server in the browser.

Creating Our API

With the seed data initialized it is time to build our API. The API I created is for multiple integrations so that it can be used for different devices etc. So, the first step in these is to create another file in the controllers folder named apiController. First two lines of code inside of this file will be to require todoModel module and body parser.

Next I’m going to use the .use method available in app and get body parser. The body parser is middleware, it will take a look at the http request for us before we handle an event. Then it’ll move on to letting our code specific to that http request continue running. So the body parser will parse out JSON out of the http request body. We’ll also make sure that it can handle url encoded data. That’s enough to make sure that we can send data nicely to our API as JSON, which will really just be string in the body of our http request and body parser will handle getting that out of there for us, and making it a JavaScript object.

After writing the code to use body parser for JSON and url encoding, we’ll have to get all the to do’s for a particular person. Another method that can be used with app is .get. The colon username means, that that can be a parameter that can change url and I can get what that value is with request.param, so I’ll just call it req, request response.

Once the .get method is set up with the right parameters/arguments I’ll use a callback to use mongoose model module. This comes with the .find method, which we’ll use to find a document that has the property username. The value of the property name is req.params.uname and uname was whatever’s in that url. Below this .find method will be an error first callback that’ll be named to dos. If there is an error, node will output it and I’m going to take that JavaScript object and let express convert it for me in JSON. That will come back as the results of my http get request.

Next we’ll build another API for individual todos. In this case we’ll get particular to dos by ID. Mongo assigns an ID to anything that’s put inside of it and mongoose provides a findById method.

After this we will now add a to do, so I’m gonna post some data that’ll be a http post. This request will be a post as its a method and if I post /api/todo, it’ll be JSON.

If I want to make a new to do or update one, I can make two different api end points. Req.body is what body parser will give me. It will add this to the request as this will be JSON data that’s converted into JavaScript object. So the body will be a JavaScript object that I can access. So if you are giving me an ID saying this is already one that exists, Ill do findByIdAndUpdate that exists in mongoose and I can say this is the ID and then I just say here’s the properties that I’m updating and here’s the new values. I’ll also need to say that hasAttachment is updated.

Last thing will be to allow a delete and this will be an http request whose method is delete.

That’s it I can get all of the to dos for a person, get a particular individual to do, a new to do, and I can delete a to do. The final thing to do is to require our apiController in the app.js.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: