//This pulls out the two functions we need from the db connection file. const { connectToDatabase, getDb } = require('./db'); // Middleware to connect to the database to see if it is working. If working it assigns the DB connection to a variable. app.use(async (req, res, next) => { await connectToDatabase(); next(); }); app.get("/", async (req, res) => { //Call our database function to get the variable holding our database connection. const db = getDb(); const collection = db.collection("projects"); const documents = await collection.find({}).toArray(); res.render("pages/homepage"); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter