-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (117 loc) · 5.03 KB
/
app.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
var http = require('http');
var fs = require('fs');
var url = require('url');
var qs = require('querystring');
var template = require('./header.js');
var mysql = require('mysql2');
var marked = require('marked');
const PASS = process.env.PASS;
console.log(typeof PASS);
var db = mysql.createConnection({
host: 'ao9moanwus0rjiex.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
user: 'bm9rie6dl9gd3hn4',
password: 'lp3alh1o25sz79ct',
database: 'nkyplzz18tmurkav'
});
db.connect();
// app start
var app = http.createServer(function (request, response) {
// 두개중 하나는 안써도 될 것 같은데..
var _url = request.url;
var pathname = url.parse(_url, true).pathname;
var table = (pathname == '/' ? 'default' : pathname.replace('/', ''));
var htmlHeader = template.build(table);
var htmlFooter = `</ul></article></div><script> </script></body></html>`;
var whiteList = ["/icon/apple-icon-57x57.png",
"/icon/apple-icon-60x60.png",
"/icon/apple-icon-72x72.png",
"/icon/apple-icon-76x76.png",
"/icon/apple-icon-114x114.png",
"/icon/apple-icon-120x120.png",
"/icon/apple-icon-144x144.png",
"/icon/apple-icon-152x152.png",
"/icon/apple-icon-180x180.png",
"/icon/android-icon-192x192.png",
"/icon/favicon-32x32.png",
"/icon/favicon-96x96.png",
"/icon/favicon-16x16.png",
"/icon/ms-icon-144x144.png",
"/icon/favicon.ico",
"/manifest.json",
"/style.css"];
console.log(pathname);
if (whiteList.includes(pathname)) {
response.end(fs.readFileSync(__dirname + pathname));
return response.writeHead(200);
}
if (pathname == '/write') {
var body = '';
request.on('data', function (data) {
body = body + data;
});
request.on('end', function () {
var dotDesc = qs.parse(body).dot_description;
var tableName = qs.parse(body).table;
if (qs.parse(body).createYN == 1) {
db.query(
`CREATE TABLE ${tableName}(
id int(11) NOT NULL AUTO_INCREMENT,
dot TEXT NOT NULL,
date DATETIME,
PRIMARY KEY (id));`,
function (err, result) { console.log(err); });
}
db.query(`INSERT INTO \`${tableName}\`(dot, date) VALUES ('${dotDesc}', NOW())`, function (err, result) {
if (err) { console.log(err); }
response.writeHead(302, { 'Location': `/${(tableName == 'default' ? '' : tableName)}` });
return response.end();
});
})
}
if (pathname == '/empty') {
var queryData = url.parse(_url, true).query;
var tableName = queryData.id;
if (queryData.row) {
db.query(`DELETE FROM \`${tableName}\` WHERE id=${queryData.row}`, function (err, result) {
if (err) { console.log(err); }
response.writeHead(302, { 'Location': `/${(tableName == 'default' ? '' : tableName)}` });
return response.end();
});
} else {
db.query(`${(tableName == 'default' ? 'TRUNCATE' : 'DROP TABLE')} \`${tableName}\``, function (err, result) {
if (err) { console.log(err); }
response.writeHead(302, { 'Location': `/` });
return response.end();
});
}
}
// 메인페이지 또는 별도의 서브 테이블에 접근하는 경우
if (pathname != '/write') {
if (pathname != '/empty') {
var expression = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
var regex = new RegExp(expression);
var dotListHTML = '';
db.query(`SELECT * FROM \`${table}\``, function (err, result) {
if (err) {
if (err.errno == 1146) {
htmlHeader = htmlHeader.replace(/value=0/, 'value=1');
}
}
try {
result.forEach((item) => {
if (item.dot.match(regex)) {
dotListHTML += `<li class="dotLi"><span class="dot"><a href="/empty?id=${table}&row=${item.id}"> </a></span><span class="dotContent"><a href="${item.dot}">${item.dot}</a></span></li>`;
} else {
dotListHTML += `<li class="dotLi"><span class="dot"><a href="/empty?id=${table}&row=${item.id}"> </a></span><span class="dotContent">${marked(item.dot)}</span></li>`;
}
});
}
catch (err) { dotListHTML = `<p>Error: ${err.errno}</p><p>Nothing to show you</p><p>Put a dot to start a new board named ${table}</p>` }
response.end(htmlHeader + dotListHTML + htmlFooter);
return response.writeHead(200);
})
}
}
});
const PORT = process.env.PORT
app.listen(PORT);