-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (60 loc) · 1.54 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
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const app = express()
mongoose.connect("mongodb://localhost:27027/todolistDB", {useNewUrlParser: true, useUnifiedTopology: true})
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static("public"))
const itemsSchema = {
name: String
}
const Item = mongoose.model('Item', itemsSchema)
const item1 = new Item({
name: "Buy milk"
})
const item2 = new Item({
name: "Tidy up"
})
const item3 = new Item({
name: "Feed the pets"
})
const defaultItems = [item1, item2, item3]
Item.insertMany(defaultItems, (err)=>{
if (err){
console.error(err)
}
})
app.get('/', (req, res) => {
var today = new Date()
/* var weekday=new Array(7);
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
weekday[3]="Thursday";
weekday[4]="Friday";
weekday[5]="Saturday";
weekday[6]="Sunday";*/
var currentDay = today.getDay().toLocaleString('en-us', {weekday: "long"});
Item.find({}, function(err, items){
if (!err){
res.render("list", {currentDay: currentDay, newItems: items})
}else{
console,log(err)
}
})
})
app.post('/', (req, res)=>{
const newItem = req.body.newItem;
Item.insertMany(newItem, (err)=>{
if(err){
console.log(err)
}
})
})
app.listen(3000, function(){
console.log("Server is running on port 3000")
})
process.on('SIGTERM', ()=>{
app.close()
})