本文介绍了猫鼬离不开互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB服务器运行在localhost:27017上,虽然我通常可以正常运行Node.js应用,但是当我断开与Internet的连接时,Mongoose抛出了错误

I have my MongoDB server running on localhost:27017, and while I can usually run my Node.js app fine, when I disconnect from the internet Mongoose throws the error

Error: failed to connect to [localhost:27017]

请注意,我仍然可以从Mongo Shell客户端连接到MongoDB服务器.另外,如果我先启动我的应用程序,然后再断开互联网连接,则我的应用程序可以脱机访问数据库.那么为什么没有互联网就无法启动?

Note that I can still connect to the MongoDB server from the Mongo shell client. Also, if I start up my app first and then lose internet connection, my app can access the database fine offline. So why can't it start up without internet?

这是完整的错误

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: failed to connect to [localhost:27017]
    at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\server.js:555:74)
    at emit (events.js:118:17)
    at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\connection_pool.js:156:15)
    at emit (events.js:110:17)
    at Socket.<anonymous> (<My App>\node_modules\mongoos
e\node_modules\mongodb\lib\mongodb\connection\connection.js:534:10)
    at Socket.emit (events.js:107:17)
    at net.js:923:16
    at process._tickCallback (node.js:355:11)
[nodemon] app crashed - waiting for file changes before starting...

措辞

推荐答案

使用127.0.0.1代替localhost.通过关闭您的wifi接口,操作系统将不再能够解析localhost.

Use 127.0.0.1 instead of localhost. By turning off your wifi interface the OS is no longer able to resolve localhost.

这篇关于猫鼬离不开互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 13:13