const mongoose = require('mongoose') const port = process.env.PORT || 3000 const env = process.env.NODE_ENV || 'development' module.exports = (app) => { let connectionString if (env === 'development') { connectionString = process.env.MONGODB_URL } else if (env === 'test') { connectionString = process.env.MONGODB_URL } else if (env === 'production') { connectionString = process.env.MONGODB_URL } mongoose .connect(connectionString, {}) .then(async function () { console.log('MongoDB Database Connected') const MyModel = mongoose.model('MyCollection', new mongoose.Schema({ name: String })) // Create a document to force MongoDB to actually create the DB and collection await MyModel.create({ name: 'test' }) app.listen(port, function () { console.log('Server Started On Port ' + port) }) }) .catch(function (err) { console.log(err) }) }