Nodemon crashed and waiting for file to be changed

29 views Asked by At
const express = require("express");

const app = express();

const PORT = process.env.PORT || 3000;

const products_routes = require("./routes/products");

app.get("/", (req, res) => {
    res.send("Hello this is my API");
})

app.use("/api/products", products_routes);

const start = async() => {
    try {
        app.listen(PORT, () => {
            console.log(`${PORT} Yes I am connected`);
        });
    } catch (error) {
        console.log(error);
    }
}

start();

This is the app.js file

const express = require("express");

const router = express.Router();

const { 
    getAllProducts, 
    getAllProductsTesting 
} = require("../controllers/products");

router.route("/").get(getAllProducts);

router.route("/testing").get(getAllProductsTesting);

This is the route.js file

const getAllProducts = async (req, res) => {
    res.status(200).json({ msg: "I am getAllProducts" });
}

const getAllProductsTesting = async (req, res) => {
    res.status(200).json({ msg: "I am getAllProductsTesting" });
}

module.exports = { 
    getAllProducts, 
    getAllProductsTesting 
};

This is the controller.js file

I am expecting that running this code will give me the response as "https://localhost:3000/api/products but it's giving me cannot find get error and now it's giving me nodemon error

1

There are 1 answers

0
Neeraj Jangir On

In which line are you exporting your router from the route.js file ? I don't see any export statements in the code you provided and you are importing it in the app.js file.