mh108
8/16/2019 - 10:43 PM

server.js

//jshint esversion: 8

const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const mongodb = require("mongodb");
const MongoClient = mongodb.MongoClient;

//const URI = process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/anycard_db";
const URI = process.env.MONGODB_URI ||"mongodb+srv://<myDefaultUserPassword>@cluster0-fvjus.azure.mongodb.net/test?retryWrites=true&w=majority"

const PORT = process.env.PORT || 5000;
const DB_NAME = process.env.DB_NAME;


app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get("/", (req, res) =>
  res.sendFile(path.join(__dirname, "index.html"))
);

app.get("/secret", (req, res) =>
  res.sendFile(path.join(__dirname, "secret.html"))
);

app.post("/secret", (req, res) => {
  MongoClient.connect(URI, { useNewUrlParser: true }, (err, client) => {
    if (err) {
      console.log(err);
    } else {
      const db = client.db(DB_NAME);
      const collection = db.collection("names");
      const entry = {
        name: req.body.name.toLowerCase(),
        card: req.body.number + "_of_" + req.body.suit
      };
      collection.insertOne(entry, (err, result) => {
        if (err) {
          console.log(err);
        } else {
          res.send("Inserted into database");
        }
      });
      client.close();
    }
  });
});

app.get("/:param*", (req, res) => {
  const name = req.url.slice(1).toLowerCase();

  MongoClient.connect(URI, { useNewUrlParser: true }, (err, client) => {
    if (err) {
      console.log(err);
      res.send(name)
    } else {
      const db = client.db(DB_NAME);
      const collection = db.collection("names");

      if (name === "deleteall") {
        collection.remove({});
        res.send("database reset");
      } else {
        collection.find({ name: name }).toArray((err, result) => {
          if (err) {
            console.log(err);
            res.send("name not found. name = " + name)
          } else if (result.length) {
            const card = result[result.length - 1].card + ".png";
            //res.send("Execution is here. card = " + card)
            //res.sendFile(path.join(__dirname + "/cards/" + card));
            res.sendFile(`${__dirname}/cards/${card}`)
          } else {
            res.send(card)
            //res.sendStatus(404);
          }

          client.close();
        });
      }
    }
  });
});

app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));