Express+mongoDB starts offline, mongodb connection fails, networking is normal, has anyone encountered this problem?

  mongodb, question

I want to learn nodeJS and learn the online tutorial. I used express+mongoDB to build a simple blog system. I accidentally did not connect to the Internet and wanted to start the project. The result was wrong. I could not start normally. Everything was normal when the network was connected normally. When the network was disconnected and offline, the project was started.MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: getaddrinfo ENOENT localhost:27017]

Error message:

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 connection information is as follows:

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, {}));

I hope the great god can give me some advice.

@enhancerBrother’s link is 404, I changed localhost to 127.0.0.1, or did I report an error


The problem has been found:
Originally I set the connection mongodb:

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

hit the targetlocalhostChanged, but session stores connectedlocalhostNo change:

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'
 })
 }));

After modification, it can be started offline, no problem!


Thank you for your help in solving the problem. Thank you, Thanks♪(・ω・)ノ