Setting up Mongo Connection from Express App on Google App-Engine
up vote
0
down vote
favorite
I am trying to setup a Mongo Connection to an Express App which would be deployed on Google App-Engine. I noticed some approaches like this:
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) return console.error(err);
db = database;
// Reuse database object in request handlers
app.get("/", function(req, res, next) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
I had some concerns about DB server outages and how it would be able to handle those. Thankfully, Node MongoDB client reconnects when DB Server is up. The only issue is that when DB server is down, the requests take a lot of time to process.
I came up with a solution, which probably works. Just wanted to know If I am doing anything wrong(Ignore the messages) or is there any better way?
let mongoConn = null;
const getMongoConn = async() => {
if(!mongoConn) {
try {
mongoConn = await MongoClient.connect("mongodb://localhost:27017", { useNewUrlParser: true });
mongoConn.on("close", () => {
console.log("Mongo Connection closed.");
mongoConn = null;
})
} catch(err) {
console.error("Mongo Connection failed.");
return null;
}
}
return mongoConn;
}
const app = express();
app.get("/", async(req, res) => {
console.log("recieved");
const conn = await getMongoConn();
try {
const db = conn.db("testDb");
const result = await db.collection("test").find().toArray();
res.status(200).send(JSON.stringify(result)).end();
} catch (e) {
res.status(404).send("DB server is down").end();
}
});
app.listen(7000, () => console.log("Server started."));
node.js mongodb google-apps-script google-app-engine
add a comment |
up vote
0
down vote
favorite
I am trying to setup a Mongo Connection to an Express App which would be deployed on Google App-Engine. I noticed some approaches like this:
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) return console.error(err);
db = database;
// Reuse database object in request handlers
app.get("/", function(req, res, next) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
I had some concerns about DB server outages and how it would be able to handle those. Thankfully, Node MongoDB client reconnects when DB Server is up. The only issue is that when DB server is down, the requests take a lot of time to process.
I came up with a solution, which probably works. Just wanted to know If I am doing anything wrong(Ignore the messages) or is there any better way?
let mongoConn = null;
const getMongoConn = async() => {
if(!mongoConn) {
try {
mongoConn = await MongoClient.connect("mongodb://localhost:27017", { useNewUrlParser: true });
mongoConn.on("close", () => {
console.log("Mongo Connection closed.");
mongoConn = null;
})
} catch(err) {
console.error("Mongo Connection failed.");
return null;
}
}
return mongoConn;
}
const app = express();
app.get("/", async(req, res) => {
console.log("recieved");
const conn = await getMongoConn();
try {
const db = conn.db("testDb");
const result = await db.collection("test").find().toArray();
res.status(200).send(JSON.stringify(result)).end();
} catch (e) {
res.status(404).send("DB server is down").end();
}
});
app.listen(7000, () => console.log("Server started."));
node.js mongodb google-apps-script google-app-engine
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to setup a Mongo Connection to an Express App which would be deployed on Google App-Engine. I noticed some approaches like this:
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) return console.error(err);
db = database;
// Reuse database object in request handlers
app.get("/", function(req, res, next) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
I had some concerns about DB server outages and how it would be able to handle those. Thankfully, Node MongoDB client reconnects when DB Server is up. The only issue is that when DB server is down, the requests take a lot of time to process.
I came up with a solution, which probably works. Just wanted to know If I am doing anything wrong(Ignore the messages) or is there any better way?
let mongoConn = null;
const getMongoConn = async() => {
if(!mongoConn) {
try {
mongoConn = await MongoClient.connect("mongodb://localhost:27017", { useNewUrlParser: true });
mongoConn.on("close", () => {
console.log("Mongo Connection closed.");
mongoConn = null;
})
} catch(err) {
console.error("Mongo Connection failed.");
return null;
}
}
return mongoConn;
}
const app = express();
app.get("/", async(req, res) => {
console.log("recieved");
const conn = await getMongoConn();
try {
const db = conn.db("testDb");
const result = await db.collection("test").find().toArray();
res.status(200).send(JSON.stringify(result)).end();
} catch (e) {
res.status(404).send("DB server is down").end();
}
});
app.listen(7000, () => console.log("Server started."));
node.js mongodb google-apps-script google-app-engine
I am trying to setup a Mongo Connection to an Express App which would be deployed on Google App-Engine. I noticed some approaches like this:
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) return console.error(err);
db = database;
// Reuse database object in request handlers
app.get("/", function(req, res, next) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
I had some concerns about DB server outages and how it would be able to handle those. Thankfully, Node MongoDB client reconnects when DB Server is up. The only issue is that when DB server is down, the requests take a lot of time to process.
I came up with a solution, which probably works. Just wanted to know If I am doing anything wrong(Ignore the messages) or is there any better way?
let mongoConn = null;
const getMongoConn = async() => {
if(!mongoConn) {
try {
mongoConn = await MongoClient.connect("mongodb://localhost:27017", { useNewUrlParser: true });
mongoConn.on("close", () => {
console.log("Mongo Connection closed.");
mongoConn = null;
})
} catch(err) {
console.error("Mongo Connection failed.");
return null;
}
}
return mongoConn;
}
const app = express();
app.get("/", async(req, res) => {
console.log("recieved");
const conn = await getMongoConn();
try {
const db = conn.db("testDb");
const result = await db.collection("test").find().toArray();
res.status(200).send(JSON.stringify(result)).end();
} catch (e) {
res.status(404).send("DB server is down").end();
}
});
app.listen(7000, () => console.log("Server started."));
node.js mongodb google-apps-script google-app-engine
node.js mongodb google-apps-script google-app-engine
edited Nov 16 at 1:56
Jamal♦
30.2k11115226
30.2k11115226
asked Nov 15 at 17:08
Sandeep Mishra
11
11
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207738%2fsetting-up-mongo-connection-from-express-app-on-google-app-engine%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown