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

Categories

0 votes
563 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:[email protected]/<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 Answer

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:[email protected]/<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)  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...