-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (73 loc) · 2.07 KB
/
index.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
90
91
92
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const cookieSession = require('cookie-session');
const Notion = require('./models/notion.js');
const User = require('./models/user.js');
const {
auth,
sendEmail,
getTime
} = require('./helper/functions.js');
const app = express();
app.use(express.urlencoded({extended : true}));
app.use(express.static('public'))
app.use(cookieSession({keys:['qqqpwoeirutylaksjdhfgzmxncbvewasscjggjcjgcmnncjhduxhgfddd']}));
app.set('view engine','ejs')
mongoose.connect(process.env.DB_STRING,{
useNewUrlParser: true,
useUnifiedTopology: true
},()=>console.log('connected to db'));
setInterval(async()=>{
const data = await Notion.find();
const now = await getTime();
const list = data.find(obj =>{
if(obj.secs < now.valueOf()){
return true;
}
});
if(list){
try{
sendEmail(list);
await Notion.deleteOne({ _id : list._id});
}catch(err){
console.error(err);
}
}
},5000);
console.log(new Date());
app.get('/signin',(req,res)=>{
res.sendFile(path.join(__dirname,'public/signin.html'));
})
app.post('/signin',async(req,res)=>{
let data = await User.find();
if(req.body.email === data[0].email && req.body.auth === data[0].password){
req.session.user = data[0].email;
}
res.redirect('/');
})
app.get('/',auth,async(req,res,err)=>{
let data = await Notion.find();
res.render('index.ejs',{data});
})
app.post('/set',auth,async(req,res)=>{
const date = new Date((req.body.time));
req.body.secs = date.valueOf();
const notion = new Notion(req.body);
await notion.save().then(e => {
console.log("success ::",e);
}).catch(e=>{
res.send(e)
})
res.redirect('/');
})
app.post('/delete',auth,async(req,res,err)=>{
id = req.body.id;
await Notion.deleteOne({_id:id});
res.redirect('/');
})
app.listen(process.env.PORT||4000,()=>{
console.log(`listing on port ${process.env.PORT||4000}`);
});