-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (151 loc) · 5.03 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const http = require('http');
const path = require('path');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer')
const {Pool} = require('./lib/DB.js');
const { response } = require('express');
const app = express();
const server = http.createServer(app);
app.set('port', 50000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
let publicPath = path.join(__dirname, 'public')
let staticware = express.static(publicPath)
app.use(staticware)
const storage = multer.diskStorage({
destination: function (req, file, cb) {``
cb(null, 'public/img/uploads/')
},
filename: function (req, file, cb) {
const ext = path.extname(file.originalname);
cb(null, Date.now() + ext);
}
})
const upload = multer({ storage: storage })
const session = require('express-session');
const MemoryStore = require('memorystore')(session);
const maxAge = 1000 * 60 * 30;
const sessionObj = {
secret: 'kong',
resave: false,
saveUninitialized: true,
store: new MemoryStore({ checkPeriod: maxAge }),
cookie: {
maxAge,
},
};
app.use(session(sessionObj));
app.use(bodyParser.urlencoded({extended: false}))
app.get('/', (request, response) => {
response.render('main')
})
app.get('/admin', async(request, response) => {
const admin = request.session.user
if(admin !== undefined) {
const sql = `SELECT * FROM contentlist`
let data = await Pool.query(sql)
response.render("admin", {data: data[0]})
} else {
response.send(`
<script>
alert('관리자 권한이 필요합니다.')
location.href = '/adminLogin'
</script>`)
}
})
app.post('/admin/del', async(request, response) => {
const {id} = request.body
const sql1 = `SELECT filename FROM contentlist WHERE id = ?`
const data = await Pool.query(sql1, [id])
const sql2 = `DELETE FROM contentlist WHERE id = ?`
await Pool.query(sql2, [id])
file = data[0][0].filename.split(',')
file.forEach(file_name => {
if (fs.existsSync("public/img/uploads/" + file_name)) {
try {
fs.unlinkSync("public/img/uploads/" + file_name);
} catch (error) {
console.log(error);
}
} else {
console.log(fs.existsSync("public/img/uploads/" + file_name))
}
})
response.send({res: 'success'})
})
app.post('/admin/accept', async(request, response) => {
const {id} = request.body
const sql = 'UPDATE contentlist SET status = ? WHERE id = ?'
await Pool.query(sql, ['o', id])
response.send({res: 'success'})
})
app.post('/getAll', async(request, response) => {
const sql = `SELECT * FROM contentlist WHERE status = ? ORDER BY id DESC`
data = await Pool.query(sql, ['o'])
response.send({data: data[0]})
})
app.get('/adminLogin', (request, response) => {
const admin = request.session.user
if(admin !== undefined) {
response.send(`
<script>
location.href = '/admin'
</script>`)
} else {
response.render("adminLogin")
}
})
app.post('/adlogin', (request, response) => {
const chid = 'admin'
const chpass = '1234'
const {id, pass} = request.body
if(chid == id && chpass == pass) {
request.session.user = true
response.send(`
<script>
location.href = '/admin'
</script>`)
} else {
response.send(`
<script>
alert('관리자 정보가 일치하지 않습니다.')
location.href = '/adminLogin'
</script>`)
}
})
app.get('/ndividualworks', async(request, response) => {
const {id} = request.query
const sql = `SELECT * FROM contentlist WHERE id = ?`
data = await Pool.query(sql, [id])
response.render("ndividualworks", {data: data[0][0]})
})
app.get('/uploadworks', (request, response) => {
response.render("uploadworks")
})
app.post('/upload', upload.array('file'), (request, response) => {
const {title, content, date, tag, scname, emailhead, emailtail, git} = request.body
const email = emailhead + "@" + emailtail
let filename = []
request.files.forEach(item => {
filename.push(item.filename)
})
filename = filename.join(',')
const sql = `INSERT INTO contentlist(title, content, filename, enddate, endyear, tag, namescnum, email, git) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
Pool.query(sql, [title, content, filename, date, date.split('-')[0], tag, scname, email, git])
response.send(`
<script>
alert('성공적으로 등록되었습니다.')
location.href = '/'
</script>`)
})
app.get('/yearworks', async(request, response) => {
const {year} = request.query
const sql = 'SELECT * FROM contentlist WHERE endyear = ? AND status = ?'
const data = await Pool.query(sql, [year, 'o'])
response.render("yearworks", {data: data[0], year})
})
server.listen(50000, () => {
console.log("Express engine 가동중");
});