【问题标题】:My terminal is showing error for following code我的终端显示以下代码错误
【发布时间】:2021-05-17 11:37:32
【问题描述】:

错误

*Server started on port 4000 Database ErrorMongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 (node:1616) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 at NativeConnection.Connection.openUri (C:\Users\HARSH KUMAR SHARMA\Desktop\MEAN\node_modules\mongoose\lib\connection.js:845:32) at C:\Users\HARMA\Desktop\MEAN\node_modules\mongoose\lib\index.js:345:10 at C:\Users\HARMA\Desktop\MEAN\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5 at new Promise (<anonymous>) at promiseOrCallback (C:\Users\HARSH KUMAR SHARMA\Desktop\MEAN\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10) at Mongoose._promiseOrCallback (C:\Users\HARMA\Desktop\MEAN\node_modules\mongoose\lib\index.js:1135:10) at Mongoose.connect (C:\Users\HARMA\Desktop\MEAN\node_modules\mongoose\lib\index.js:344:20) at Object.<anonymous> (C:\Users\HARMA\Desktop\MEAN\app.js:10:10) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47 (Usenode --trace-warnings ...to show where the warning was created) (node:1616) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag*--unhandled-rejections=strict`(见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (node:1616) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

app.js 文件

const express = require('express');
const path = require ('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport  = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');

// Connect to Database
mongoose.connect(config.database, {useNewUrlParser:true , useUnifiedTopology:true}); 

// On Connection
mongoose.connection.on('connected' , ()=>{
    console.log('Connected to DataBase' + config.database);
});

// On Error
mongoose.connection.on('error' , (err)=>{
    console.log('Database Error' + err);
});

const app = express();

const users = require('./routes/users');

// Port Number
const port = 4000 ;

// CORS Middleware
app.use(cors());

//Set Static Folder
app.use(express.static(path.join(__dirname , 'public')));

//bodyParser Middleware
app.use(bodyParser.json());

app.use('/users' , users);

//INDEX Route
app.get('/' , (req ,res) => {
    res.send('invalid Endpoint');
})

// Start Server  
app.listen(port , () => {
    console.log('Server started on port ' + port);
})

database.js 文件

module.exports = {
    database: 'mongodb://localhost:27017/meanauth',
    secret : 'yoursecret'
};

【问题讨论】:

    标签: javascript node.js express mongoose


    【解决方案1】:

    您是否检查过您的 mongo 是否正常工作,并且您收到的节点错误是因为您没有处理错误,您可以使用 try catch 来处理您的错误

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2021-10-22
      • 2013-12-22
      • 2014-09-14
      • 1970-01-01
      • 2014-10-22
      相关资源
      最近更新 更多