skip to Main Content

Need help, I am trying to learn nodejs from youtube. I have created below but not getting any response from browser. Can you please check below code and advise what I have missed??
if I removed the courseModel.find … from course.js and just write resp.send("XYZ") its showing on browser otherwise nothing is showing. please help

seems something is missing in mangodb connection string

–index.js

const connection = require('./model/connection');
const express = require('express');
const app = express();
const handleBars = require('express-handlebars');
const bodyParser = require('body-parser');
const http = require('http');
const path = require('path');
const courseController = require('./controllers/course');

const server = http.createServer(app);

app.use(bodyParser.urlencoded({
extended: true,
}))


app.use("/course", courseController);

server.listen(3900, () => {
console.log("server is in running mode.");
});

–connection.js

const mongoose = require('mongoose');
var conn = mongoose.createConnection("mongodb://localhost:27017/learning", (err) => {
if (!err) {
    console.log("Mongo db connected");
} else {
    console.log(err);
}
});

const courseModels = require("./course.model");

–course.model.js

const mongoose = require('mongoose');

var CourseSchema = new mongoose.Schema({
courseName: {
    type: String,
    required: 'Required'
},
courseId: {
    type: String,
},
courseDuration: {
    type: String,
}
});

module.exports = mongoose.model("Course", CourseSchema);

–course.js

const express = require('express');
const mongoose = require('mongoose');

const router = express.Router();
const courseModel = mongoose.model("Course");


router.get("/lists", (req, resp) => {

courseModel.find((err,docs)=>{
    if(!err){
        resp.send(docs)
    }
});

});

module.exports = router;

–logs

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    issue resolved after updating the connection.js as below

    const mongoose = require('mongoose');
    
    mongoose.connect('mongodb://localhost:27017/learning',
    {
    useNewUrlParser: true,
    // useFindAndModify: false,
    useUnifiedTopology: true
    },  console.log("mongo db connected")
    
    );
    

  2. Insted of .

    server.listen(3900, () => {
    console.log("server is in running mode.");
    });
    

    use :

    app.listen(3900, () => {
    console.log("server is in running mode.");
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search