Skip to content

Instantly share code, notes, and snippets.

@vladi-strilets
Last active April 5, 2020 18:08
Show Gist options
  • Save vladi-strilets/8dc29cef59b3cea3a64647ffb5ce2bc3 to your computer and use it in GitHub Desktop.
Save vladi-strilets/8dc29cef59b3cea3a64647ffb5ce2bc3 to your computer and use it in GitHub Desktop.
mongodb client handler node.js #2
// routes/someroute.js
const express = require("express");
const router = express.Router();
const getClient = require("../db");
router.get("/", async (req, res) => {
try {
const client = await getClient();
const db = client.db("YOUR_BD_NAME");
const collection = db.collection("YOUR_COLLECTION_NAME");
collection.find({}).toArray((err, data) => {
res.send(data);
}
} catch (err) {
res.status(500).send('Something broke!');
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment