MongoDB connection is now established once.
This commit is contained in:
parent
c8b37a43ca
commit
0315e0222a
1 changed files with 15 additions and 13 deletions
|
@ -41,7 +41,6 @@ function generateUniqueShortenedURLID(db, callback) {
|
|||
var id = generateShortenedURLID();
|
||||
db.collection("urls").find({id: id}).toArray(function(err, result) {
|
||||
if (err) {
|
||||
if(db.close) db.close();
|
||||
callServerError(500, err);
|
||||
return;
|
||||
}
|
||||
|
@ -58,6 +57,10 @@ function escapeRegExp(string) {
|
|||
}
|
||||
|
||||
function connectMongo(callback) {
|
||||
// customvar3 is a MongoDB database object
|
||||
if (customvar3) {
|
||||
callback(customvar3);
|
||||
} else {
|
||||
MongoClient.connect(mongoURL, function(err, db) {
|
||||
if (err) {
|
||||
callServerError(500, err);
|
||||
|
@ -65,10 +68,12 @@ function connectMongo(callback) {
|
|||
}
|
||||
var dbo = db.db(dbname);
|
||||
dbo.createCollection("urls", function(err, dbres) {
|
||||
customvar3 = dbo;
|
||||
callback(dbo);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function formatTemplate(templateName, templateData, callback) {
|
||||
fs.readFile(__dirname + "/../templates/" + templateName + ".template", function(err, data) {
|
||||
|
@ -132,7 +137,6 @@ if (href.match(/^\/admin\/?$/)) {
|
|||
connectMongo(function(db) {
|
||||
db.collection("urls").find({url: purl}).toArray(function(err, result) {
|
||||
if (err) {
|
||||
if(db.close) db.close();
|
||||
callServerError(500, err);
|
||||
return;
|
||||
}
|
||||
|
@ -142,7 +146,6 @@ if (href.match(/^\/admin\/?$/)) {
|
|||
generateUniqueShortenedURLID(db, function(id) {
|
||||
db.collection("urls").insertOne({id: id, url: purl}, function(err, dbres) {
|
||||
if (err) {
|
||||
if(db.close) db.close();
|
||||
callServerError(500, err);
|
||||
return;
|
||||
}
|
||||
|
@ -160,7 +163,6 @@ if (href.match(/^\/admin\/?$/)) {
|
|||
connectMongo(function(db) {
|
||||
db.collection("urls").find({id: id}).toArray(function(err, result) {
|
||||
if (err) {
|
||||
if(db.close) db.close();
|
||||
callServerError(500, err);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue