Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here all my code giving me error of trigger Uncaught Exception in terminal on creating routes code file - package.json, index.js , posts.js check import "./" this all good but still giving me error.how to ride of this and what cause of this error

 {
          "name": "server",
          "version": "1.0.0",
          "description": "",
          "main": "index.js",
          "type": "module",
          "scripts": {
            "start": "nodemon index.js"
          },
          "keywords": [],
          "author": "",
          "license": "ISC",
          "dependencies": {
            "body-parser": "^1.19.0",
            "cors": "^2.8.5",
            "express": "^4.17.1",
            "mongoose": "^5.11.13",
            "nodemon": "^2.0.7"
          }
        }
    
    
    import express from "express";
    import  bodyParser from "body-parser"
    import cors from "cors"
    import mongoose from "mongoose";
    
    import postRoutes from "./routes/posts"
    
    
    const app = express();
    
    
    app.use("/posts", postRoutes);
    
    //Compuslory dependenci
    app.use(bodyParser.json({limit:"30mb", extended:"true"}));
    app.use(bodyParser.urlencoded({limit:"30mb", extended:"true"}));
    app.use(cors());
    
    const CONNECTION_URL = "mongodb+srv://username:passwd@cluster0.igadz.mongodb.net/<dbname>?retryWrites=true&w=majority";
    const PORT = process.env.PORT || 9000;
    
    mongoose.connect(CONNECTION_URL, {useNewUrlParser: true, useUnifiedTopology:true})
       .then(() => app.listen(PORT, () => console.log(`server runing on ${PORT}`)))
       .catch((err) => console.log(err))
    
    mongoose.set('useFindAndModify', false)  

import express from "express";

const router = express.Router();

router.get('/', (req, res) => {
    res.send('this dont  work')
}) 

 export default router;
question from:https://stackoverflow.com/questions/65880279/node-express-giving-me-error-module-not-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
574 views
Welcome To Ask or Share your Answers For Others

1 Answer

its giving me error because javaScript read code line by and my mistake was a create route above the cors or express its should be below

const app = express();




//Compuslory dependenci
app.use(bodyParser.json({limit:"30mb", extended:"true"}));
app.use(bodyParser.urlencoded({limit:"30mb", extended:"true"}));
app.use(cors());

const CONNECTION_URL = 
   "mongodb+srv://username:passwd@cluster0.igadz.mongodb.net/<dbname>? 
retryWrites=true&w=majority";
const PORT = process.env.PORT || 9000;

mongoose.connect(CONNECTION_URL, {useNewUrlParser: true, useUnifiedTopology:true})
   .then(() => app.listen(PORT, () => console.log(`server runing on ${PORT}`)))
   .catch((err) => console.log(err))
app.use("/posts", postRoutes);

mongoose.set('useFindAndModify', false)  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...