且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查猫鼬连接状态而无需创建新连接

更新时间:2023-12-03 21:09:28

由于猫鼬模块导出了单例对象,因此您不必在test.js中进行连接即可检查连接状态:

Since the mongoose module exports a singleton object, you don't have to connect in your test.js to check the state of the connection:

// test.js
require('./app.js'); // which executes 'mongoose.connect()'

var mongoose = require('mongoose');
console.log(mongoose.connection.readyState);

就绪状态为:

  • 0:已断开连接
  • 1:已连接
  • 2:连接
  • 3:断开连接