javascript 猫鼬连接nodejs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 猫鼬连接nodejs相关的知识,希望对你有一定的参考价值。

const mongoose = require('mongoose');

function getConnection() {
    const dbUri = process.env.mongoDB_HOST;
    mongoose.connect(dbUri, {
        useNewUrlParser: true
    });
    // CONNECTION EVENTS

    // When successfully connected
    mongoose.connection.on('connected', function () {
        console.log('Mongoose default connection open to ' + dbUri);
    });

    // If the connection throws an error
    mongoose.connection.on('error', function (err) {
        console.log('Mongoose default connection error: ' + err);
    });
    // When the connection is disconnected
    mongoose.connection.on('disconnected', function () {
        console.log('Mongoose default connection disconnected');
    });

    // If the Node process ends, close the Mongoose connection 
    process.on('SIGINT', function () {
        mongoose.connection.close(function () {
            console.log('Mongoose default connection disconnected through app termination');
            process.exit(0);
        });
    });
}
module.exports = {
    getConnection
};

以上是关于javascript 猫鼬连接nodejs的主要内容,如果未能解决你的问题,请参考以下文章

使用异步 javascript 和猫鼬连接性能表

javascript 如何使用猫鼬连接mongodb?

Mongodb的僧侣vs猫鼬

Mongodb的僧侣vs猫鼬

连接停止时如何处理猫鼬数据库

猫鼬,nodejs,全文搜索