-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (93 loc) · 2.97 KB
/
server.js
File metadata and controls
115 lines (93 loc) · 2.97 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// A: package.json!
// what command do we run to start an npm project?
// A: npm init
// what does the below chunk of code do?
// A: imports libraries
let fishs;
let fishObject;
let fishArray;
const express = require("express"); // imports express
const multer = require("multer"); // imports multer -- handles file upload
const bodyParser = require("body-parser"); // imports body parser -- allows us to have a body in server request
const cookieParser = require("cookie-parser");
// translates bits and bytes (literal memory and data) to something readable by the server
const urlEncodedParser = bodyParser.urlencoded({ extended: true });
// what is app?
// A: instance of express
const app = express();
// what is this configuring?
// A: destination for where files should be uploaded
const upload = multer({
dest: "public/uploads",
});
// let database = new nedb({
// filename: "database.txt",
// autoload: true,
// });
// let fishcollected = new nedb({
// filename: "fishcollected.txt",
// autoload: true,
// });
// let templeResponse = new nedb({
// filename: "templeResponse.txt",
// autoload: true,
// });
// what do each of these statements do?
const fs = require("fs");
app.use(express.static("public")); // set the default folder for any static files such as assets, css, html
app.use(urlEncodedParser); // middleware to make sure the bits and bytes can be understood by the app
app.set("view engine", "ejs"); // allows us to use ejs
fs.readFile("public/fishdatabase.json", function (err, data) {
fishObject = JSON.parse(data);
fishArray = fishObject.fishes;
});
// what is this?
// A: route that handles when the client makes a request to /
app.get("/", (req, res) => {
// response.send("server working");
// what steps do we need in order to use a template ejs file?
//
res.render("index.ejs", {});
// make sure to comment out the res.send() code above
});
app.post("/collectfish", (req, res) => {
res.redirect("/collect");
});
app.get("/fishingSite", (req, res) => {
// response.send("server working");
// what steps do we need in order to use a template ejs file?
let randomFish = getRandomFish();
res.render("fishing-site.ejs", { randomFish });
});
function getRandomFish() {
const randomIndex = Math.floor(Math.random() * fishArray.length);
let randomFish = fishArray[randomIndex];
return randomFish;
}
app.get("/random", (req, res) => {
console.log("accesing random");
let randomFish = getRandomFish();
res.json(randomFish);
});
app.get("/collect", (req, res) => {
res.render("collect.ejs", {});
});
// res.render("collect.ejs", {});
// make sure to comment out the res.send() code above
app.get("/temple", (req, res) => {
res.render("temple.ejs", {});
});
app.listen(6001, () => {
console.log("server started on port 6001");
});
// app.listen(5000, function() {
// })
// secret comment for later in the demo:
// @seald-io/nedb
// function foo() {
// console.log("hello")
// }
// const foo = () => {
// console.log("hello")
// }
// foo()