Home > Blockchain >  Cannot find module for routes
Cannot find module for routes

Time:03-20

I'm getting the error: Cannot find module '.app/routes/favourite.routes' and I'm not sure why this error is occuring. I've double checked the naming and the path and it appears to be the exact same.

Here is my server.js file

const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");

const app = express();

var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

const db = require("./app/models");
const Role = db.role;

db.mongoose
  .connect(`mongodb srv://tami00:[email protected]/test?retryWrites=true&w=majority`, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log("Successfully connect to MongoDB.");
    initial();
  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

// simple route
app.use('/favourite', require('.app/routes/favourite.routes'));

// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

function initial() {
  Role.estimatedDocumentCount((err, count) => {
    if (!err && count === 0) {
      new Role({
        name: "user"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'user' to roles collection");
      });

      new Role({
        name: "creator"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'creator' to roles collection");
      });

      new Role({
        name: "watcher"
      }).save(err => {
        if (err) {
          console.log("error", err);
        }

        console.log("added 'watcher' to roles collection");
      });
    }
  });
}

and here is my favourite.routes.js file. I have no issue with the other 2 routes.

const express =  require('express');
const router = express.Router();
const{Favourite} = require("../models/favourite.model");
const {auth} = require("../middlewares/authJwt");

router.post("/favouriteNumber", auth, (req, res) => {
    Favourite.find({"movieId": req.body.movieId})
        .exec((err, favourite) => {
            if(err) return res.status(400).send(err)
            res.status(200).json({success: true, favouriteNumber: favourite.length})
        })
})

router.post("/favourited", auth, (req, res) => {
    Favourite.find({"movieId": req.body.movieId, "userFrom": req.body.userFrom})
        .exec((err, favourite) => {
            if(err) return res.status(400).send(err) 

            let result = false;
            if(favourite.length !== 0) {
                result = true
            }

            res.status(200).json({success: true, favourited: result});


        })
})

router.post("/addToFavourite", auth, (req, res) => {
    
    const favourite = new Favourite(req.body)

    favourite.save((err, doc) => {
        if(err) return res.json({success: false, err})
        return res.status(200).json({success: true, doc})
    })
})

router.post("/removeFavorite", auth, (req, res) => {
    
    Favourite.findOneAndDelete({movieId: req.body.movieId, userFrom: req.body.userFrom})
        .exec((err, doc) => {
            if(err) return res.json({success: false, err})
            return res.status(200).json({success: true, doc})
        })
})

module.exports = router;

This is the folder structure of my project

Project
 |   
  -- backend
   |  
     -- app
       |  
        -- config (folder)
        -- controllers (folder)
        -- middlewares (folder)
        -- models (folder)
        -- routes (folder)
       |   |  
       |    -- favourite.routes.js
       |    -- auth.routes.js
       |    -- user.routes.js
        -- server.js (file)

In the command line I

C:\Users\tamia\Downloads\proj\backend> npm start

CodePudding user response:

I think u missed / before app Try using ./app/routes/favourite.routes

  • Related