![](/img/trans.png)
[英]I get this syntax error when running my server.js trying to connect to my sql db
[英]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}`));
我收到此錯誤:無法連接 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)
在 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 },代碼:未定義 }`
在這一行中將localhost
更改為0.0.0.0
mongodb://localhost:27017/articlesdb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.