今天在学习express的时候遇到了一个问题。

使用mongoose的时候用到了如下的语句。
mongoose.connect('mongodb://localhost:27017/react-express-api');
结果在启动后台时报错
[MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: getaddrinfo ENOTFOUND localhost localhost:27017]
一开始以为是mongodb没有启动,后来发现可以用mongo shell,只能在网上搜查解决办法,发现将localhost 替换为127.0.0.1就可以了.
原来是在mac下我的localhost没有和本机的ip绑定,解决办法很简单,去修改本机的hosts文件即可.
sudo open -a TextEdit /etc/hosts
127.0.0.1 localhost