检查数据库是否已连接到localhost:9200与节点
我在本地使用nodejs服务器。此应用程序连接到端口localhost:9200上的mongodb。
我想知道是否有可能在代码中检查节点服务器开头的数据库连接,这样我就不必等到节点尝试从服务器找到一些东西才能找到没有联系
像这样的东西:
checkConnection('localhost:9200', (err) => {
if (err)
console.log('can't connect to database')
else
console.log('database connected')
}
谢谢您的回答。
回答如下:我想您正在使用mongoose来管理您的Mongo数据库。在这种情况下,你可以尝试这句话:
let mongoose = require('mongoose');
mongoose.Promise = global.Promise;
// Database connection
mongoose.connect('mongodb://localhost:9200/<name of your database>')
.then(() => {
console.log("Database conection Ok");
})
.catch(err => console.log(err));
如果数据库运行正常,您也可以启动服务器,您可以尝试使用以下句子:
let mongoose = require('mongoose');
let app = require('./app');
let port = 3800;
// Database connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:9200/<name of your database>')
.then(() => {
console.log("Database conection Ok");
// Server creation
app.listen(port, () => {
console.log("Server running in http://localhost:3800");
});
})
.catch(err => console.log(err));
检查数据库是否已连接到localhost:9200与节点
我在本地使用nodejs服务器。此应用程序连接到端口localhost:9200上的mongodb。
我想知道是否有可能在代码中检查节点服务器开头的数据库连接,这样我就不必等到节点尝试从服务器找到一些东西才能找到没有联系
像这样的东西:
checkConnection('localhost:9200', (err) => {
if (err)
console.log('can't connect to database')
else
console.log('database connected')
}
谢谢您的回答。
回答如下:我想您正在使用mongoose来管理您的Mongo数据库。在这种情况下,你可以尝试这句话:
let mongoose = require('mongoose');
mongoose.Promise = global.Promise;
// Database connection
mongoose.connect('mongodb://localhost:9200/<name of your database>')
.then(() => {
console.log("Database conection Ok");
})
.catch(err => console.log(err));
如果数据库运行正常,您也可以启动服务器,您可以尝试使用以下句子:
let mongoose = require('mongoose');
let app = require('./app');
let port = 3800;
// Database connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:9200/<name of your database>')
.then(() => {
console.log("Database conection Ok");
// Server creation
app.listen(port, () => {
console.log("Server running in http://localhost:3800");
});
})
.catch(err => console.log(err));