Understanding NodeJS by building a Task App
In this post, we are going to create an awesome Task App in which we are going to use MongoDB to store data.
You can watch this in video format on my YouTube channel.
We can use MongoDB cloud database of Atlas for the same, but we are going to use the locally store MongoDB for the same. So, head over to https://www.mongodb.com/ and hover over Products and then click on Community Server.
After that download the MongoDB for Windows.
Install mongodb by going through the setup process. After that install, the awesome GUI tool of MongoDB Compass and open it. To start the local instance of MongoDB, click on Connect button.
We can also run mongodb commands from the shell. Run the command mongo from the command line and then db.version() to check the version number.
Now, we will start with our project. So, create a new folder task-manager and change to it. After that create a empty node project by npm init -y command.
We are also installing the mongoDB package version 3 in our project.
Now, create a file mongoConn.js and add the below content in it. Here, we are first importing MongoClient from mongodb and then giving the local connection url and a database name.
We are then using MongoClient to connect to the database. We need to use the useNewUrlParser and useUnifiedTopology, or else the log gives warnings.
After that if, we have error we are returning back or else we are connecting to the database. After that we are inserting a new collection users in the database, with insertOne command to insert a single document.
After that in then and catch block, just showing the result or error.
When we run the command node mongoConn.js from the terminal, it will give back our result.
Now, in MongoDB Compass, we can see the result. We have also created some more documents with the same command.
Now, we can insert many documents in one go, with insertMany(). Notice, that here, we need to pass an array of object.
Now, it also got inserted sucessfully in the database.
Now, we will learn how to find a document in colection by findOne(). Notice that it only returns the first document found.
If we want to find all of the documents, including the duplicate ones, we can use the find() method. It send us back an array, so we need to use the toArray() before passing it to the then.
Now, we will learn how to update a single document. We do this by updateOne() function. In this we are targetting an id, so we have to import the ObjectID with new. After that we are just setting with $set.
Now, the data will be updated in database.
Similary, we can update many documents with updateMany().
And, our matched two data are updated.
Now, we will learn to delete data. Here, we are using the deleteOne to delete single record and deleteMany to delete multiple records.
Now, those three records are deleted from our database.
Now, we will add mongoose in our app, which allows us to add models in our code. These models are stored in the MongoDB database.
We are also going to add validator package, which helps us to do the validation in a mongoose model.
Next, we are adding express because we will create route with it. We will also add nodemon as dev dependencies, as we don’t want to restart server after every changes.
Now, we will create the real Task manager app. So, create a server.js file in the root directory. Also, a folder db and models in root directory. In the db folder create a file mongoose.js. Inside the models folder, create task.js and user.js file.
Now, in the mongoose.js file, we will import mongoose and then connect to our local database.
Now, in the task.js file we will have the model of the task record. Here, we have two fields of desc and done. One is of type string and other of boolean.
We have used required in desc, which means it’s a required filed. Also, we are using trim, which means if the user is passing empty space after the field, it will be trimmed.
The done field also have a default of false.
Now, we will create user.js file. Here, we are also using the validator. Using the validator, we can directly validate whether a email is valid. In the password, we are also validating, whether the user have passed the pass word.
Now, we will finally add code in the server.js. Here, we are first importing express, then the mongoose connection. After that we are also importing the User and the Task model.
We also need to use the app, port and do express.json(), because we send data as json.
Next, we are creating two POST routes for users and tasks. Here, we are just taking the data in request body and then using save() method to save it in the database.
Lastly, we are listening at provided port.
Now, before running our app, we will update the package.json file to update the scripts. Here, the start will be for heroku deployment and the dev, we be for local run.
Now, run the server.js file from the terminal.
Next, in Postman we will create a route for http://localhost:8000/users where in the body, we are sending the data. Notice that we are using a wrong password, which is not allowed.
So, we are not able to add the user in database.
Not, we are sending a correct password and we are able to get the success message and also 201 created.
Now, if we also check in our database the data is created.
Next, we have also added two tasks successfully in the database.
Now, we will add the GET routes for users and tasks. We are also adding the routes to get the individual user and task.
Now, if we go to http://localhost:8000/tasks route in postman, we will get all the tasks.
Similarly, we can get the individual tasks by giving the id.
Similarly the users GET routes are working like same. Now, we will create the PUT routes, which are used to update the data in any document.
Here, we are taking the id and then using the mongoose findByIdAndUpdate by passing the parameters in body.
Also, notice that we are using the new syntax of async..await.
Now, in the postman, send a PUT request with the id http://localhost:8000/users/61e16bd0b94a65199a4d5f12. Here, we are updating the password and getting the status of 200 ok.
Now, the password is updated in document.
Next, we are also adding the function for the PUT of the task.
Lastly, we will add the routes for delete, in which we just need to pass the id.
Now, in postman we just need to pass the id like below http://localhost:8000/tasks/61e186b9b94a65199a4d5f16
We complete our small task manager app, which we are going to continue in the next post. The code for the same can be found here.