Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const port = process.env.PORT || 3000;
  3.  
  4. // Require mongo. Here we're pulling MongoClient out of the require and immediately storing
  5. // it in a variable called mongo.
  6. const mongo = require('mongodb').MongoClient;
  7.  
  8. // This is the URL for connecting to the database. As you can see, we're allowing this
  9. // to be set from the outside, or setting it ourselves. We've used this strategy before
  10. // with process.env.PORT for Heroku, and we're doing the exact same thing here.
  11. let url = process.env.MONGODB_URI || "mongodb://localhost:27017/my-database";
  12.  
  13. const app = express();
  14.  
  15. // This will store our database connection
  16. let dbClient;
  17.  
  18. // Very important that we pass an asynchronous function here to app.get, so that we can
  19. // use await inside it.
  20. app.get("/", async (req, res) => {
  21.  
  22.     try {
  23.         // Get a collection, which is like a single bundle of stuff within our database
  24.         const collection = await dbClient.collection("pagemeta");
  25.  
  26.         // The pagemeta collection holds only one item, so we can call findOne, passing
  27.         // in empty search criteria, to just get that one thing
  28.         const item = await collection.findOne({});
  29.  
  30.         // Increment the number of pageviews, if we've seen the page before
  31.         let pageviews = 1;
  32.         if (item) pageviews = item.pageviews + 1;
  33.  
  34.         // Update the item in the database, inserting if it's not there. That's what
  35.         // upsert means.
  36.         await collection.updateOne({}, {
  37.             $set: {
  38.                 pageviews: pageviews
  39.             }
  40.         }, {
  41.             upsert: true
  42.         });
  43.        
  44.         // Finally, report back the number of pageviews
  45.         res.send(`This page has been viewed ${pageviews} times`);
  46.        
  47.     } catch (e) {
  48.         res.status(500).send("Some kind of terrible error happened");
  49.         console.log(e);
  50.     }
  51. });
  52.  
  53. // We're not using async/await here because there's no top-level await
  54. // First connect to the database given the url.
  55. mongo.connect(url, {
  56.     useNewUrlParser: true,
  57.     useUnifiedTopology: true
  58. }).then((client) => {
  59.  
  60.     // Store the client connection so that we can use it later
  61.     dbClient = client.db();
  62.  
  63.     // Finally, start the server like normal
  64.     app.listen(port, () => {
  65.         console.log(`Express app listening on port ${port}`);
  66.     });
  67. }).catch((err) => {
  68.     console.log("Couldn't connect to the database");
  69.     console.log(err);
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement