-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (49 loc) · 1.27 KB
/
index.js
File metadata and controls
60 lines (49 loc) · 1.27 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
import express from "express";
import bodyParser from "body-parser";
import mongoose from 'mongoose';
import * as data from './data.js';
const date = new Date();
const app = express();
const port = 3000;
var tasks=[];
const taskSchema= new mongoose.Schema({
name:String
});
const Task=mongoose.model("Task",taskSchema);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
mongoose.connect(data.path(), {useNewUrlParser: true});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
app.get("/", async (req, res) => {
try{
var temptasks= await Task.find();
tasks=[];
temptasks.forEach((task)=>{tasks.push(task.name)});
}
catch(err){
tasks=[];
console.log(err);
}
res.render("../index.ejs",{today:date.getDate()+"."+(date.getMonth()+1)+"."+date.getFullYear(),tasks:tasks});
});
app.post("/submiJjt", (req, res) => {
var val=req.body.newItem;
const task=new Task({
name:val,
});
task.save()
res.redirect("/");
});
app.post("/clear", (req, res) => {
tasks.length=0;
Task.deleteMany({_id:{$ne:null}})
.then(function (tasks) {
tasks.length=0;
})
.catch(function (err) {
console.log(err);
});
res.redirect("/");
});