-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (108 loc) · 4.35 KB
/
server.js
File metadata and controls
129 lines (108 loc) · 4.35 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require('dotenv').config();
const express =require('express')
const app = express()
const port = process.env.PORT
app.set('view engine', 'ejs')
const helmet = require('helmet');
app.use(helmet({
contentSecurityPolicy: false, //csp header disable, 인라인 스크립트사용
}));
app.use(express.static('public'))
app.use(express.urlencoded({extended:true}))
const { ObjectId } = require('mongodb') /* findOne( { _id : ObjectId('611e61ee9eee')} ) */
const { MongoClient } = require('mongodb')
let db
const url = process.env.DB_URL //db url
new MongoClient(url).connect().then((client)=>{
console.log('DB연결성공')
db = client.db('bin')
}).catch((err)=>{
console.log(err)
})
app.get('/', (req,res)=>{
res.render("new")
})
app.post('/save', async (req,res)=>{
const value = req.body.value
const ttlOption = req.body.ttlOption
const createdAt = new Date() // 현재 시간을 생성합니다.
let result
const indexInfo = await db.collection('post').indexInformation()
if (indexInfo.createdAt_1) {
await db.collection('post').dropIndex("createdAt_1")
}
try{
if (ttlOption === '30s'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 30 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '1m'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 60 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '10m'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 600 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '30m'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 1800 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '1h'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '3h'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 10800 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '1day'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 86400 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else if (ttlOption === '7day'){
await db.collection('post').createIndex({ "createdAt": 1 }, { expireAfterSeconds: 604800 })
result = await db.collection('post').insertOne({ value, createdAt })
}
else{
// result = await db.collection('post').insertOne({ value }) //Naver
return res.status(400).send('Invalid TTL option.');
}
res.redirect(`/display/${result.insertedId}`) //결과반환
}catch(e){
res.render("new",{ value })
}
})
app.get('/display/:id', async (req,res)=>{
try{
const id = req.params.id
const post = await db.collection('post').findOne({_id:new ObjectId(id)})
const document = `${post.value}`
res.render('display', { code : document})
}
catch(e){
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'})
res.write("<script>alert('Page Not Found'); window.location.href = '/';</script>")
}
})
app.get('/display/:id/raw', async (req, res) => {
try {
const id = req.params.id;
const post = await db.collection('post').findOne({ _id: new ObjectId(id) });
if (!post) {
return res.status(404).send('코드를 찾을 수 없습니다.');
}
// Content-Type을 text/plain으로 설정
res.set('Content-Type', 'text/plain');
res.send(post.value);
} catch (error) {
console.error(error);
res.status(500).send('서버 오류');
}
});
app.get('/about', (req,res)=>{
res.render('about',)
})
app.listen(port, ()=>{
console.log("서버가열렸습니다",port);
})