skip to Main Content

Below is my implementation:

main file

const express  = require("express");
const path = require("path");;
const app = express();
const hbs = require("hbs");
require("./db/conn")
const port = process.env.PORT || 3000 ;

const static_path = path.join(__dirname,"../public");
const template_path = path.join(__dirname,"../templates/views");
const partials_path = path.join(__dirname,"../templates/partials");

app.use(express.static(static_path))
app.set("view engine", "hbs");
app.set("views",template_path);
hbs.registerPartials(partials_path);

app.get("/"  , (req,res)=> {
    res.render("index")
}
);

app.listen(port, () => {
    console.log(`server is running at port no ${port}`)
})

conn.js file for mongodb connection

const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/youtubeRegistration" , {
    // useNewUrlParser : true,
    // useUnifiedTopology: true,
    // useCreateIndex : true
}).then(() =>{
    console.log(`connection successful`);
}).catch((e)=>{
    console.log(`no connection ${e}`)
})

error

no connection MongooseServerSelectionError: connect ECONNREFUSED ::1:27017, connect ECONNREFUSED 127.0.0.1:27017

service stopped with the following error:

Windows could not start the mongoDB server (mongoDB) service on local computer

Error 1067 : the process terminated unexpectedly

2

Answers


  1. Go to Win Services and look for MongoDB Server (MongoDB)

    Disable it .. then restart the service

    Login or Signup to reply.
  2. Here is your revised conn.js file

    const mongoose = require("mongoose");  
      
    // Connection URI  
    const uri = "mongodb://localhost:27017/youtubeRegistration";  
      
    // Connect to MongoDB  
    mongoose.connect(uri, {  
        useNewUrlParser: true,  
        useUnifiedTopology: true,  
    })  
    .then(() => {  
        console.log("Connection successful");  
    })  
    .catch((e) => {  
        console.error("No connection", e);  
    });  
    

    Below is your modified MainFile

    const express = require("express");  
    const path = require("path");  
    const hbs = require("hbs");  
    require("./db/conn.js");  // Ensure this is the right path to your conn.js  
      
    const app = express();  
    const port = process.env.PORT || 3000;  
      
    // Set up paths  
    const static_path = path.join(__dirname, "../public");  
    const template_path = path.join(__dirname, "../templates/views");  
    const partials_path = path.join(__dirname, "../templates/partials");  
      
    // Middleware  
    app.use(express.static(static_path));  
    app.set("view engine", "hbs");  
    app.set("views", template_path);  
    hbs.registerPartials(partials_path);  
      
    // Routes  
    app.get("/", (req, res) => {  
        res.render("index");  
    });  
      
    // Start server  
    app.listen(port, () => {  
        console.log(`Server is running at port no ${port}`);  
    });  
    

    Troubleshooting steps:

    1. Ensure MongoDB is Installed and Running
    2. Check the MongoDB Service (restart if necessary)
    3. Check the Connection URI mongoose.connect('mongodb://localhost:27017/yourDatabaseName', { useNewUrlParser: true, useUnifiedTopology: true });
    4. Check if Firewall or Antivirus aren’t blocking connections
    5. Check MongoDB Configuration mongod.conf
    6. Reinstall MongoDB
    7. Try launching MongoDB from commandline "C:Program FilesMongoDBServer<version>binmongod.exe" --dbpath "C:datadb"
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search