-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy.js
More file actions
64 lines (56 loc) · 1.98 KB
/
my.js
File metadata and controls
64 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const path = require("path");
require("dotenv").config({
path: path.resolve(__dirname, "./.env"),
});
const { MongoClient, ServerApiVersion } = require("mongodb");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
let collection;
const uri = process.env.MONGO_CONNECTION_STRING;
const client = new MongoClient(uri, { serverApi: ServerApiVersion.v1 });
const router = express.Router();
(async () => {
try {
await client.connect();
const databaseName = client.db("animalDB");
collection = databaseName.collection("animals");
await collection.deleteMany({});
console.log("MongoDB connnected");
} catch (e) {
console.error(e);
process.exit(1);
}
}) ();
app.get("/style.css", (request, response) => {
response.sendFile(path.join(__dirname, "style.css"));
});
app.use(bodyParser.urlencoded({ extended: false }));
app.set("view engine", "ejs");
app.set("views", path.resolve(__dirname, "templates"));
app.get("/", (request, response) => {
response.render("index");
})
router.post("/randomAdd", async (request, response) => {
const nickname = request.body.nickname;
const res = await fetch("https://random-animal-api.p.rapidapi.com/api/random-animal",
{method: "GET",
headers: { "x-rapidapi-key": process.env.RAPIDAPI_KEY,
"x-rapidapi-host": "random-animal-api.p.rapidapi.com"}});
const data = await res.json();
console.log("API response:", data);
await collection.insertOne({animal: data.city, nickname: nickname})
response.redirect("/all")
})
router.get("/all", async (request, response) => {
const animals = await collection.find({}).toArray();
let answer = "";
animals.forEach((item) => {
answer += `<li>Animal: ${item.animal}, Nickname: ${item.nickname}</li>`;
});
response.render("all", { items: answer });
})
app.use("/", router);
app.listen(3000, () => {
console.log("Server is listening on port 3000");
});