我想重用MongoDB連接。我是知道的How to reuse mongodb connection in node.js 我想acheive的使用相同的承諾和蒙戈司機V2如何通過Promise重用MongoDB連接
目前我有連接到數據庫,每這使得它緩慢的請求。這是我的代碼
"use strict"
var app = require('./utils/express')();
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
//Actually I 'am connecting to MongoLab
var url = 'mongodb://localhost/my-mongo';
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('ParkMe app is running on port', app.get('port'));
});
app.get('/location/create', function(req,res,next){
MongoClient.connect(url).then(function(db) {
return db.collection('parkme_parkingLots').find({}).toArray().then(function (docs) {
return docs;
});
});
});
我想要做的事,如:
"use strict"
var app = require('./utils/express')();
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://nidhind:[email protected]:51635/my-mongo';
var db = MongoClient.connect(url).then(function(db) {
return db;
});
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('ParkMe app is running on port', app.get('port'));
});
app.get('/location/create', function(req,res,next){
db.collection('parkme_parkingLots').find({}).toArray().then(function (docs) {
return docs;
});
});