-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (87 loc) · 2.26 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
93
94
95
96
97
98
99
require('dotenv').config();
const express = require('express');
const cookieSession = require('cookie-session');
const mongoose = require('mongoose');
const Name = require('./models/Names')
const PORT = process.env.PORT || 3000;
const app = express();
mongoose.connect(process.env.DB_STRING, (e) => {
app.listen(PORT, () => {
console.log("on port::" + PORT);
})
})
app.set('view engine','ejs');
app.use(express.static('./public'));
app.use(express.urlencoded({ extended:true}))
app.use(cookieSession({
name: 'session',
keys: ['mykeyysjsjsj'],
maxAge: 2000 * 60 * 60 * 1000
}))
app.get('/',(req,res)=>{
if(req.session.id){
res.redirect('/profile/'+req.session.id);
}
res.render("index");
})
app.get('/create',(req,res)=>{
res.render('create')
})
app.post('/create',(req,res)=>{
const name = new Name({
name:req.body.name
});
const id = name.save();
req.session.id = name.id;
res.redirect('/profile/'+name.id)
})
app.get('/profile/:id',async(req,res)=>{
if(!req.session.id){
res.redirect('/create');
}
if(req.session.id !== req.params.id){
res.redirect('/comment/'+req.params.id);
}
const obj = {
name: "",
link: "",
results: []
}
try{
const data = await Name.findById(req.session.id);
obj.name = data.name;
obj.link = process.env.HOST_URL+'/comment/'+ data.id,
obj.results = data.texts || [];
}catch(e){
console.log(e);
res.redirect('/create')
}
res.render('view',obj);
})
app.get('/comment/:id',async(req,res)=>{
const obj = {
name: "",
id: ''
}
try {
const data = await Name.findById(req.params.id);
obj.name = data.name;
obj.id = data.id;
res.render('write', obj);
} catch (error) {
res.send('faield');
console.log(error);
}
})
app.post('/comment',async(req,res)=>{
try {
const oldData = await Name.findById(req.body.id);
const texts = oldData.texts;
texts.push(req.body.text)
const data = await Name.findByIdAndUpdate(req.body.id, { $set: { texts:texts } });
res.redirect('/');
} catch (error) {
console.log(error)
res.redirect('comment');
}
})