-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
175 lines (156 loc) · 4.57 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
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const data = fs.readFileSync("./database.json");
const conf = JSON.parse(data);
const mysql = require("mysql");
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
database: conf.database,
multipleStatements: true,
});
app.use(cors()); //Cross Origin Resource Sharing
connection.connect();
app.get("/api/test", (req, res) => {
connection.query(`SELECT * FROM TEST`, (err, rows, fields) => {
res.send(rows);
});
});
app.get("/", (req, res) => {
res.send("hello world!");
});
app.get("/api/book/:shelf_num", (req, res) => {
connection.query(
`SELECT * FROM BOOK WHERE shelf_num=${req.query.shelf_num}`,
(err, rows, fields) => {
res.send(rows);
}
);
});
app.get("/api/shelf/:id", (req, res) => {
const id = req.query.user_id;
connection.query(
`SELECT * FROM BookShelf WHERE user_id="${id}"`,
(err, rows, fields) => {
res.send(rows);
}
);
});
app.post("/api/test", (req, res) => {
//console.log(req.body.params);
const coord = req.body.params;
console.log(coord);
const sql = `UPDATE TEST SET pos_x=${coord.x}, pos_y=${coord.y} WHERE id=${coord.id}`;
connection.query(sql, (err, rows, fields) => {
res.send(rows);
});
});
app.post("/api/shelf", (req, res) => {
const sql = "INSERT INTO BookShelf VALUES (?, null, ?)";
const id = req.query.user_id;
const shelf_name = req.query.shelf_name;
const params = [id, shelf_name];
connection.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
app.post("/api/book", (req, res) => {
let sql = "INSERT INTO BOOK VALUES (null, ?, ?, ?, ?, ?, ?, ?)";
let image = req.body.image;
let title = req.body.title;
let author = req.body.author;
let publisher = req.body.publisher;
let price = req.body.price;
let user_id = req.body.user_id;
let shelf_num = req.body.shelf_num;
let params = [image, title, author, publisher, price, user_id, shelf_num];
connection.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
app.post("/api/sign", (req, res) => {
console.log(req.body.data.pass);
let sql =
"SELECT * FROM register WHERE ID='" +
req.body.data.email +
"' and private_key='" +
req.body.data.pass +
"'";
let email = req.body.data.email;
let password = req.body.data.pass;
//let usernameRegex = /^[a-z0-9]+$/;
let params = [email, password];
console.log(params);
connection.query(sql, params, (err, rows, fields) => {
if (err) {
throw err;
}
if (rows.length > 0) {
console.log(rows[0].ID);
return res.json({ loginresult: true, name: rows[0].ID });
} else {
console.log("f3");
return res.send({ loginresult: false });
}
});
});
app.post("/api/register", (req, res) => {
let sql = "INSERT INTO register VALUES (?,?)";
let email2 = req.body.email2;
let password2 = req.body.password2;
//let usernameRegex = /^[a-z0-9]+$/;
let params = [email2, password2];
console.log(params);
connection.query(sql, params, (err, rows, fields) => {
if (!err) {
res.send("success");
console.log("not error");
} else {
console.log("error");
return res.status(400).json({
// HTTP 요청에 대한 리스폰스 (json 형식으로)
error: "duplicate EMAIL",
code: 1,
});
}
});
});
app.delete("/api/shelf/:id", (req, res) => {
const id = req.body.id;
const shelf_num = req.body.shelf_num;
params = [shelf_num, shelf_num];
const sql =
"DELETE FROM BookShelf WHERE shelf_num=?;DELETE FROM BOOK WHERE shelf_num=?";
connection.query(sql, params, (err, rows, fields) => {
if (err) {
console.log(err);
} else {
console.log("delete done");
}
});
});
app.delete("/api/books/:id", (req, res) => {
const id = req.params.id;
const sql = `DELETE FROM BOOK WHERE id=${id}`;
connection.query(sql, (err, rows, fields) => {
console.log("delete done");
});
});
app.post("/api/books/:id", (req, res) => {
const sql = `UPDATE BOOK SET title='${req.body.title}', author='${req.body.author}', publisher='${req.body.publisher}' WHERE id=${req.body.id}`;
connection.query(sql, (err, rows, fields) => {
console.log("edit done");
if (err) {
console.log(err);
}
});
});
app.listen(port, () => console.log(`Listening on port ${port}`));