javascript Zeit Now的Anycard-fcc server.js文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Zeit Now的Anycard-fcc 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}`));

以上是关于javascript Zeit Now的Anycard-fcc server.js文件的主要内容,如果未能解决你的问题,请参考以下文章

[Now] Configure secrets and environment variables with Zeit’s Now

json 适用于Anycard-fcc App的Zeit Now配置文件

zeit-now v2 + apollo-server-express:操场错误:无法访问服务器

使用 Zeit Now 的 Next js + Express 部署问题

在使用 ZEIT Now 部署的 Next.js 应用程序中使用绝对导入

如何将在 zeit 中运行的 node.js 与 postgresql 连接起来?