Please make sure that you have completed and UNDERSTOOD the following tutorial https://zellwk.com/blog/crud-express-mongodb/ before starting this exercise.
You may refer to this tutorial throughout this exercise.
Done? Great, Let's start!
-
Create a folder for this project. Initialize npm to manage dependencies. Create a
server.jsfile inside this folder. -
Create an app that uses
expressandbody-parserand listens on port3000 -
Install Nodemon and create a script in
package.jsonto use it -
Install
MongoDB, connect to it and create a Database onMongoLab. -
Make sure that your app
onlylistens when the database is connected -
Create an
index.htmlfile that contains a form as follows:- action
'/posts'and methodpost - input field of type
textwith placeholder"Title"and a name"title" - textarea of type
textwith placeholder"Text", a name"text", rows"5", and cols"50" - input of type
submit
- action
-
Send the above file on the main page
'/' -
Using
save()function, save the data submitted through this form in the database. Remember that your form action is'/posts'and your form method ispost -
Using
find()andtoArray()functions, display the data you have in the database on the console -
Install
ejsand set it as theview engine -
Create a directory named
viewsand createindex.ejsfile inside it -
Copy the code you have inside
index.htmltoindex.ejsand deleteindex.html -
using
render()function and anejscode that you will write, display the data you have in the database on the main page'/'as follows:- The form be displayed on the
leftpart of the page - Posts should be displayed on the
rightpart of the page as:h3for title andpfor text of each post
- The form be displayed on the
Congratulations! You successfully applied everything you learned in the tutorial.
Now that you can add posts, let's make the user able to delete and edit them.
-
Delete
- Add an anchor
<a>under each post of href'delete/id' with id being the id of the post selected. Remember thatMongoLabsets a default id for each post - Handle this link in
server.jsusingapp.post(/delete/:id)where you delete the selected post from database usingdelete()function and render to'/'page
- Add an anchor
-
Edit
- In views, create
update.ejspage that contains a form as follows:- action
'/update/id'and methodpost - input field of type
textwith placeholder"Title", name"title", and value of the old title - textarea of type
textwith placeholder"Text", name"text", rows"5", cols"50", and value of the old text - input of type
submit
- action
- Add an anchor
<a>under each post of href'edit/id' with id being the id of post selected. Remember thatMongoLabsets a default id for each post - Handle this link in
server.jsusingapp.post(/edit/:id)where you find the post that the user looking for usingfindOne()function and render the result toupdate.ejs - The form in
update.ejshas'/update/id'as action, this should also be handled inserver.jsusingapp.post(/update/:id)UsefindOneAndUpdate()function to set the new values forTitleandTextas entered by the user
Congratulations! You can now Add, edit and delete posts from a database.
- In views, create
Now, the user shall be able to sort the posts by ID (default sorting), title (alphabetical order) or date.
- Delete all your posts to avoid any type of conflict.
- Go to
index.ejsand add the following:- An input field to the form of type
hiddeniddateand namedate - The following script
This step allowed us to send then = new Date(); // get current date // formatting the date h = n.getHours(); M = n.getMinutes(); y = n.getFullYear(); // getMonth() counts January as 0 so we need +1 m = n.getMonth() + 1; d = n.getDate(); //check if it's AM or PM if (h < 12){ document.getElementById("date").value = h + ":" + M + " AM " + m + "/" + d + "/" + y; } else { document.getElementById("date").value = h + ":" + M + " PM " + m + "/" + d + "/" + y; }
datewhen the post was added - An input field to the form of type
- Now, in
index.ejsadd 3 anchors<a>above the posts as follows:- Sort by Id with
href = "/default" - Sort by Title with
href = "/title" - Sort by Date with
href = "/date"
- Sort by Id with
- Handle these links in
server.jsby adding:sorting?toapp.get('/'). The question mark means that this parameter is optional (not required). Usingfind()andsort()functions, sort the posts based on which anchor is pressed.
Congratulations! You created a full Blog.
Only display the first 3 words of each post on the index.ejs page and give the option for the user to read full post on another page read.ejs. Use split(), slice() and join() functions.
For those who want to have more fun, here are some additional tasks you might find interesting.
Limit the number of posts displayed on one page to 3 posts. If there are additional posts, the user has the option to move between pages (there will be 2 anchors next and previous). Use limit() and skip() functions.