如何通过Express和Mongoose连接到本地网络中的MongoDB
我正在用express和mongoose设置服务器,我希望它可以在本地网络中存在的其他计算机中使用。 Mongodb配置文件中的bind_ip变量已经设置为0.0.0.0。
const connection = mongoose
.connect(
"mongodb://192.168.254.104/db",
{
useNewUrlParser: true
}
)
.then(() => console.log("Connected to MongoDB"))
.catch(error => console.log(error));
我尝试在手机上进行连接,但服务器响应称未找到数据库。
回答如下:这是mongodb连接字符串的格式:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]]][/[database][?options]]
如果要连接到本地主机数据库,则字符串将像这样:
"mongodb://localhost:27017/mydb"
其中“ mydb”是本地计算机上的数据库名称。