The most basic project you can create using Node.js and Express.js or Nest.js is a simple REST API.
For this purpose, I'd like to suggest you build a book directory, where you would need to create endpoints, using the four most basic methods: GET,POST, PUT and DELETE.
You'd use GET for getting all books or getting only one book by id. With the POST method, you can add a new book to the list. You'd need the PUT method for updating the existing book, and it's evident that with the DELETE method, you will remove the book from the list.
For this kind of application, you don't have to use the database, and for the first app, you can start with data collected as a JSON file.
git clone https://github.com/Aabhashzzz1/books-directory.git
cd books-directory
npm i (npm install)
To start the project
Production Use
npm start
Development use
npm run dev
Use Postman to check REST APIs calls
Get Request
Getting all the books data
router.get('/', (req,res) => {
res.json(booksData);
});
Getting only one or specific books data by id
router.get('/:id', (req,res) => {
const { id } = req.params;
res.json(booksData.filter((ele) => ele.id === parseInt(id)));
});
Post Request
Posting books data in json file/format
router.post('/', (req,res) => {
const info = req.body;
console.log(info);
booksData.push(info);
res.json({ message: 'The book has been uploaded.' });
});
Put Request
Putting/Updating books data in json file
router.put('/:id', (req,res) => {
const { id } = req.params;
const info = req.body;
console.log(info);
booksData.forEach((book, index) => {
if (book.id === parseInt(id)) {
booksData[index] = info;
}
});
res.json({ message: `The book data with id ${id} has been updated` });
});
Delete Request
Deleting only one or specific books data by id
router.delete('/:id', (req,res) => {
const { id } = req.params;
booksData.forEach((book, index) => {
if (book.id === parseInt(id)) {
booksData.splice(index);
}
});
console.log(booksData);
res.json({ message: `The book data with id ${id} has been deleted` });
});