-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (49 loc) · 1.36 KB
/
app.js
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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 3000;
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
const uri = process.env.ATLAS_URI;
mongoose.connect(uri);
const itemsSchema = {
name: String
};
const Item = mongoose.model("Item", itemsSchema);
app.get("/", function(req, res) {
const date = new Date();
const options = {
weekday: "long",
month: "long",
day: "numeric"
}
const day = date.toLocaleDateString("en-US", options);
Item.find({}, function(err, foundItems) {
res.render("list", {listTitle: day, newListItems: foundItems});
});
});
app.post("/", function(req, res) {
const itemName = req.body.newItem;
const item = new Item ({
name: itemName
});
item.save();
res.redirect("/");
});
app.post("/delete", function(req, res) {
const checkedItemID = req.body.checkbox;
Item.deleteOne({_id: checkedItemID}, function(err) {
if(!err) {
res.redirect("/");
}
});
});
app.get("/about", function(req, res) {
res.render("about");
});
app.listen(port, function() {
console.log("Server has started successfully.")
});