Amazing, after we finished configuring our model and database using Mongodb Atlas, now we are going to create and manage the route used for the middleware of our project.
Folder hierarchy |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner (Part 6)
First, we have to make a folder called routes and create a file named note.js inside the folder. Once finished, we can write our codes like below.
const express = require('express'); //load module express
//Express is a module framework for Node that you can use for applications that are based on server/s that will "listen" for any input/connection requests from clients.
const router = express.Router(); //Creating Router() object
//load model
const notes_collection = require('../models/Notes');
//router
//to get all the notes
router.get('/', async (req, res) => {
try { // using try block catch because we are using asynchronous
const notes = await notes_collection.find(); //using await to get data collection from model
res.json(notes); //to get json object
// res.render('note_home');
} catch (error) {
res.json({ message: error }); //will get
// res.render('note_home', { notes });
}
});
//to get one node the notes
router.get('/:id', async (req, res) => {
try {
const notes = await notes_collection.find({ _id: req.params.id }); //to get one collection of data regarding id
res.json(notes);
// res.render('note_home');
} catch (error) {
res.json({ message: error });
// res.render('note_home', { notes });
}
});
//to save notes
router.post('/', async (req, res) => {
const note = new notes_collection({ //create an object containing required key and its value
title: req.body.title,
content: req.body.content
});
try {
const saveNote = await note.save(); //save to mongodb
res.json(saveNote); //show json data after saving
} catch (error) {
res.json({ message: error });
}
});
//to delete
router.get('/:id', async (req, res) => {
const id = req.params.id;
try {
const delNote = await notes_collection.deleteOne({ _id: id });
res.json(delNote);
} catch (error) {
json.res({ message: eror });
}
});
//to update
router.post('/:id', async (req, res) => {
try {
const note = await notes_collection.updateOne(
{ _id: req.params.id },
{
$set: {
title: req.body.title,
content: req.body.content
}
}
);
res.json(note);
} catch (error) {
res.json({ message: error });
}
});
module.exports = router; //will export all the routes
Press ctrl + s to save and npm start to run the project.
Now we are ready to test our simple note Using Node js, Express, Mongodb.
Testing the API
To test the APIs of our note project, we need postman. Postman is a Google Chrome app for interacting with HTTP APIs. It presents you with a friendly GUI for constructing requests and reading responses.. Therefore, It is a powerful tool used to test web services. It was developed for sending HTTP requests in a simple and quick way. You can download it in Google chrome – just type Postman.
Now, in Postman choose the type of HTTP method with your needs (to update and save we will use POST, but to get information from database we must use GEET method). Next, define your url with the correct port. After that choose x-www-form-url-encoded and write the key (must be the same like the attributes in database) and give the value of each attributes then click send. If you encounter error please let me know by giving comments : ) .
Just try to do like image below. First you have to make sure that you already start your project. The results will be written in JSON format.
Using Postman for POST method / to Save
This method will be saving new inputs from the user.
post method to save data |
Using Postman for GET method /notes or /notes/number_id
This method will show all (or one specific) data / information saved in the database.
get method to retrieve data |
Using Postman for POST method to update /notes/number_id
this method will allow us to do update data
post method to update data |
the result after updating -> of course we have to check using get method /notes |
Use get to delete
the get method is also able to help deleting data. See image below to delete
Get method to delete |
Result after deleting -> of course w e have to check using get method /notes |
Don’t worry, I know that using postman must be so frustrating and of course, ‘not-user-fiendly enough’. I will also implement our own views here (using HTML).
Final Part : Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner (Part 7)
Table Content
title | Link |
---|---|
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 1 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 2 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 3 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 4 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 5 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 6 | Link |
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap Part 7 | Link |
Tag :
Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner, Express, Mongodb and Bootstrap For Beginner, Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner
EmoticonEmoticon