I’m building a Book Store app and was trying to make it with the MVC model. I have set up routes for CRUD operations. However, I’m running into an issue where my bookRoute is not defined due to a module not found error.
This is my book.route file
import express from 'express'
import { getBook,getBooks,createBook,updateBook,deleteBook } from '../controllers/book.controller.js';
const bookRoute = express.Router();
//Book Controller Function
bookRoute.get('/',getBooks);
bookRoute.get('/:id',getBook);
bookRoute.post('/',createBook);
bookRoute.put('/:id',updateBook);
bookRoute.delete('/:id',deleteBook)
export default bookRoute;
And this is my Controller Exports
import { Book } from "../models/Cat.model";
export const getBooks = async (req, res) => {
try {
const books = await Book.find();
res.status(200).json(books);
} catch (error) {
res.status(500).json({ message: error.message });
}
}
export const getBook = async (req, res) => {
try {
const id = req.params.id;
const book = await Book.findById(id);
if (!book) {
res.status(404).json({ message: 'Not Found!' });
}
} catch (error) {
res.status(500).json({ message: error.message })
}
}
export const createBook = async (req, res) => {
try {
const newBook = await Book.create(req.body);
res.status(200).json(newBook);
} catch (error) {
res.status(500).json({ message: error.message })
}
}
export const updateBook = async (req, res) => {
try {
const id = req.params.id;
const updatedBook = await Book.findByIdAndUpdate(id);
if (!updatedBook) {
res.status(404).json({ message: 'Not Found!' });
}
res.status(200).json(updatedBook);
} catch (error) {
res.status(500).json({ message: error.message })
}
}
export const deleteBook = async (req,res)=>{
try {
const id = req.params.id;
const deletedBook = await Book.findByIdAndDelete(id);
if (!deletedBook) {
res.status(404).json({message:'Not Found!'});
}
} catch (error) {
res.status(500).json({message: error.message});
}
}
Also in case you were wondering this is the structure my app was taking:
I have tried putting the whole Path in the from ‘./’ Yet even with that it doesnt work, I am not sure if I am doing something wrong. Well of course I am but I can’t seem to know what.