鍍金池/ 問答/數(shù)據(jù)庫  HTML/ express+mongoDB在離線狀態(tài)下啟動,mongodb連接失敗,聯(lián)網(wǎng)狀態(tài)

express+mongoDB在離線狀態(tài)下啟動,mongodb連接失敗,聯(lián)網(wǎng)狀態(tài)下正常,有人遇到過這個問題嗎?

想學習nodeJS,學著網(wǎng)上的教程使用express+mongoDB做了一個簡單的博客系統(tǒng),偶然間未聯(lián)網(wǎng)想啟動項目,結果報錯了,不能正常啟動,網(wǎng)絡連接正常的時候一切正常,當斷開網(wǎng)絡,離線狀態(tài)的時候,啟動項目就報MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: getaddrinfo ENOENT localhost:27017]

報錯信息:


C:\Users\pan\Documents\workSpace\node_blog\node_modules\_mongodb@2.2.35@mongodb\lib\mongo_client.js:421
          throw err
          ^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: getaddrinfo ENOENT localhost:27017]
    at Pool.<anonymous> (C:\Users\pan\Documents\workSpace\node_blog\node_modules\_mongodb-core@2.1.19@mongodb-core\lib\topologies\server.js:336:35)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (C:\Users\pan\Documents\workSpace\node_blog\node_modules\_mongodb-core@2.1.19@mongodb-core\lib\connection\pool.js:280:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (C:\Users\pan\Documents\workSpace\node_blog\node_modules\_mongodb-core@2.1.19@mongodb-core\lib\connection\connection.js:189:49)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! blog@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the blog@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\pan\AppData\Roaming\npm-cache\_logs\2018-03-28T05_43_28_490Z-debug.log

圖片描述

mongodb連接信息如下:

module.exports = {
  cookieSecret: 'myblog',
  db: 'blog',
  host: 'localhost'
};
var settings = require('../settings'),
    Db = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server = require('mongodb').Server;
module.exports = new Db(settings.db, new Server(settings.host, 27017, {}));

還望大神指點一二

回答
編輯回答
拽很帥

@enhancer 兄弟的鏈接是404,我把localhost改成127.0.0.1了,還是報錯


問題已經找到:
原來我把連接mongodb的設置:

module.exports = {
  cookieSecret: 'myblog',
  db: 'blog',
  host: 'localhost'
};

中的localhost改了,但是session存儲連接的localhost沒改:

app.use(session({
    secret: settings.cookieSecret,
    key: settings.db,
    cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
    store: new MongoStore({
      db: settings.db,
      url: 'mongodb://127.0.0.1:27017/blog'
    })
}));

修改后在離線狀態(tài)可以啟動,沒問題!


感謝諸位幫忙解決問題,謝啦Thanks?(?ω?)?
2018年6月12日 13:35
編輯回答
未命名

你可以試試用node ./bin/www啟動項目,而不是cnpm start啟動項目。
如果可以,請把你的package.json文件貼出來看看

2017年1月25日 22:19
編輯回答
逗婦乳

把 localhost 換成 127.0.0.1 吧

2017年8月31日 04:55