Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

routes - Node express giving me error module not found

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

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

1 Reply

0 votes
by (71.8m points)

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)  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...