【问题标题】:nodejs. close connection节点。紧密连接
【发布时间】:2012-01-18 16:26:28
【问题描述】:

我有带有 mongodb(mongoose) 的 nodejs 应用程序。像这样:

 var mongoose = require('mongoose');
 var express = require('express');
 mongoose.connect('mongodb://localhost/my_database');

 .....

 var DocumentSchema = new Schema({
     title  : String
    , body  : String
    , date  : Date
 });

 var Document = mongoose.model('Document', DocumentSchema);

 app.get('/documents', function(req, res) {
    // get all docs
    Document.find({}, function(err, docs) {

       docs = docs.map(function(d) {
           return {title: d.title, id: d._id};
        });

       res.render('documents/index.jade', {documents: docs});
    });
 });

所以所有用户都使用单个 MongoDB 连接。我应该在每次请求后关闭连接吗?

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    不,你永远不应该关闭连接并重新打开它,让它保持打开状态,除非你希望你的进程消耗更多的 CPU / RAM。

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2015-03-02
      • 2022-10-06
      • 2019-07-16
      相关资源
      最近更新 更多