Skip to content

Instantly share code, notes, and snippets.

@yamachanyama
Created March 21, 2018 05:03
Show Gist options
  • Save yamachanyama/02907e4ef71b19ec358ec9349be2af7c to your computer and use it in GitHub Desktop.
Save yamachanyama/02907e4ef71b19ec358ec9349be2af7c to your computer and use it in GitHub Desktop.
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var CONTACTS_COLLECTION = "contacts";
var app = express();
app.use(bodyParser.json());
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/test", function (err, client) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = client.db();
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// CONTACTS API ROUTES BELOW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment