简体   繁体   English

我正在尝试从 mongodb 连接我的 server.js express api 但它显示一些错误我不明白

[英]I am trying to connect my server.js express api from mongodb but it showing some error i don't understand it

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)我收到此错误:无法连接 MongooseServerSelectionError: connect ECONNREFUSED::1:27017 at Connection.openUri (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\connection.js:824:32 ) 在 D:\Vue 实践项目\instant-search-project\node_modules\mongoose\lib\index.js:381:10 在 D:\Vue 实践项目\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 ZCADCDEDB567ABAE643E15DCFiseOrCallbackOrCallback (D:\Vue Practice Project\instant-search-project\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10) :\Vue 实践项目\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%20Practice%20Project/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 }`在 Mongoose.connect (D:\Vue 实践项目\instant-search-project\node_modules\mongoose\lib\index.js:380:20) 在 file:///D:/Vue%20Practice%20Project/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,逻辑会话超时分钟:null },代码:未定义 }`

change the localhost to 0.0.0.0 in this line mongodb://localhost:27017/articlesdb在这一行中将localhost更改为0.0.0.0 mongodb://localhost:27017/articlesdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行我的 server.js 尝试连接到我的 sql db 时出现此语法错误 - I get this syntax error when running my server.js trying to connect to my sql db 我正在尝试将 mongodb 服务器与节点连接 - I am trying to connect mongodb server with node 我正在尝试从Rest API中获取json数据以进行反应,但是它显示了一些错误 - I am trying to fetch json data from Rest API into react, but it showing some error 将Express API数据库与server.js文件分开 - Separate express api database from server.js file 我试图了解为什么此JS代码段无法正常工作,可以使用一些可添加到WP中的指南 - I am trying to understand why this JS snippet isn't working, could use some guidance adding to WP 如何将代码从server.js中抽象到Express应用程序的子目录中? - How can I abstract code out of server.js into subdirectories in my Express app? 从 server.js 快速导入应用程序给我一个错误 - Express importing app from server.js is giving me an error 我不明白为什么我会收到此错误 - I don't understand why I am getting this error 我是否错误地导出了对象? JS 新手不明白为什么测试失败 - Am I exporting my objects incorrectly? new to JS don't understand why test is failing 我不理解Flux中Dispatcher.js中的某些语法 - I don't understand some syntax in Dispatcher.js in Flux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM