无法修复mongodb的一些错误

时间:2017-10-06 13:35:31

标签: node.js mongodb mongoose gulp

以下是我尝试使用gulp运行项目时收到的错误消息。我想这个问题是因为mongodb造成的。

Gulp is running my app on PORT: 8000

events.js:160
  throw er; // Unhandled 'error' event
  ^
MongoError: failed to connect to server [localhost:27017] on first connect 
[MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\H SHASHANK 
KUMAR\Desktop\code\node_modules\mongodb-core\lib\topologies\server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\H SHASHANK KUMAR\Desktop\code\node_modules\mongodb-core\lib\connection\pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\H SHASHANK 
KUMAR\Desktop\code\node_modules\mongodb-
core\lib\connection\connection.js:187:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

以下是可能包含错误的代码。

var express = require('express')
mongoose = require('mongoose'); // creating reference for express

var db = mongoose.connect('mongodb://localhost/bookAPI'); //using mongoose 
to connect to mongo db.

var Book = require('./models/bookModel'); //mongo db uees models to retrive 
data.

var app = express(); // creating an instance of express to run app.

var port = process.env.PORT || 3000;

var bookRouter = express.Router();

bookRouter.route('/Books')
.get(function(req, res){
    Book.find(function(err, books){
        if(err)
            console.log(err);
        else
            res.json(books);
    });     
});

app.use('/api', bookRouter);



app.get('/', function(req, res){
 res.send('welcome to my API');
});

app.listen(port, function(){
 console.log('Gulp is running my app on PORT: '+port);
});

我已经尝试在堆栈溢出的其他可能的帖子中搜索错误,但我没有找到任何修复我的问题。

0 个答案:

没有答案