[英]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.