Home > Blockchain >  I am trying to connect my server.js express api from mongodb but it showing some error i don't
I am trying to connect my server.js express api from mongodb but it showing some error i don't

Time:09-16

Here is my code of server.js

    import express from 'express';
    
    import bodyParser from 'body-parser';
    
    import userRoutes from './routes/users.js';
    
    import mongoose from 'mongoose';
    
    const app = express();
    const PORT = 4000;
    
    app.use(bodyParser.urlencoded({ extended: true }));
    
    app.use('/users', userRoutes);
    
    mongoose.Promise = global.Promise;
    
    mongoose.connect("mongodb://localhost:27017/articlesdb" ,{
        useNewUrlParser: true
      })
      .then(() => {
        console.log("database connected");
      })
      .catch(err => {
        console.log("Could not connect", err);
      });
      
    app.get('/', (req, res) => {
        console.log('[TEST]!');
        res.send('Hello from homepage.');
    });
    
    app.listen(PORT, () =`enter code here`> console.log(`Server is Running on port: http://localhost:${PORT}`));

And I am getting this error: Could not connect MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at Connection.openUri (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\connection.js:824:32) at D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\index.js:381:10 at D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5 at new Promise () at promiseOrCallback (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10) at Mongoose._promiseOrCallback (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\index.js:1234:10)
at Mongoose.connect (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\index.js:380:20) at file:///D:/Vue Practice Project/instant-search-project/server.js:27:10 at ModuleJob.run (node:internal/modules/esm/module_job:193:25) at async Promise.all (index 0) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: null, maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined }`

CodePudding user response:

change the localhost to 0.0.0.0 in this line mongodb://localhost:27017/articlesdb

  • Related