-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (71 loc) · 2.02 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require("dotenv").config();
const express = require('express');
const bodyparser = require('body-parser');
const path = require('path');
const currentDate = require(path.join(__dirname, "date.js"));
const mongoose = require("mongoose");
var _ = require("lodash");
const app = express();
const portNum = process.env.PORT || 8800;
mongoose.connect(process.env.URI);
const TodoListSchema = new mongoose.Schema({
work: {
type: String
},
dateinfo: {
type: String,
required: true
}
});
var listname = "";
var List = " "
app.set("view engine", "ejs");
app.use(bodyparser.urlencoded({ extended: true }))
app.use(express.static("public"));
app.get('/', (req, res) => {
mongoose.connection.client.db("todoList").listCollections().toArray().then(val => {
res.render('index', { userCreatedLists: val })
})
});
app.get('/lists/:pageName', (req, res) => {
listname = _.lowerCase(req.params.pageName);
List = mongoose.model(listname, TodoListSchema);
List.find({}).then(val => {
res.render("home", { dateInfo: currentDate.getDate(), newWorksTodo: val, pagename: listname });
});
})
app.post('/addlists', async (req, res) => {
var name = _.lowerCase(req.body.listName)
mongoose.connection.client.db("todoList").createCollection(name, function (err, listcollection) {
if (err) {
console.log(err);
}
}).then(x => {
res.redirect('/');
})
})
app.post('/lists/:pageName', (req, res) => {
const listiteam = new List({
work: req.body.newWork,
dateinfo: currentDate.getDate()
})
listiteam.save();
res.redirect(`/lists/${listname}`);
});
app.post('/delete', (req, res) => {
List.findByIdAndDelete({ _id: req.body.checkboxValue }).then(err => {
res.redirect(`/lists/${listname}`);
});
})
app.post("/deletelist", (req, res) => {
mongoose.connection.db.dropCollection(req.body.listval, function (err, result) {
if (err) {
console.log(err);
}
}).then(x => {
res.redirect("/");
})
})
app.listen(portNum, () => {
console.log(`Succefully connected to ${portNum} :)`);
})