-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
197 lines (182 loc) · 4.79 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
187
188
189
190
191
192
193
194
195
196
197
const bcrypt = require("bcrypt");
const express = require("express");
const app = express();
const path = require("path");
const passport = require("passport");
const session = require("express-session");
const LocalStrategy = require("passport-local").Strategy;
const { ObjectId } = require("mongodb");
const http = require("http").createServer(app);
const { Server } = require("socket.io");
const io = new Server(http);
app.use(express.static("public"));
require("dotenv").config();
app.use(express.urlencoded({ extended: true }));
const MongoClient = require("mongodb").MongoClient;
app.use(express.json());
var cors = require("cors");
app.use(cors());
app.use(
session({ secret: "비밀코드", resave: true, saveUninitialized: false })
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, "/build")));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "/build/index.html"));
});
var db;
MongoClient.connect(process.env.DB_URL, function (err, client) {
if (err) return console.log(err);
db = client.db("shop");
http.listen(process.env.PORT, function () {
console.log("listening on 8080");
});
});
app.post("/signin", function (req, res) {
const myPlaintextPassword = req.body.pw;
const saltRounds = 10;
bcrypt.hash(myPlaintextPassword, saltRounds, function (err, hash) {
db.collection("login").insertOne(
{
id: req.body.id,
pw: hash,
name: req.body.name,
address: req.body.address,
tel: req.body.tel,
email: req.body.email,
},
function (err, result) {
console.log("저장완료");
}
);
});
res.send("전송완료");
});
app.post("/logout", function (req, res, next) {
req.logout(function (err) {
if (err) {
return next(err);
}
res.send("0");
});
});
app.post(
"/login",
passport.authenticate("local", { failureRedirect: "/login" }),
function (req, res) {
res.render("/");
console.log("성공");
}
);
app.post("/chat", function (req, res) {
let a = db
.collection("chat")
.find({ member: ObjectId(req.body.id) })
.toArray();
if (a) {
a.then((result) => {
res.send({ data: result });
});
} else {
db.collection("chat").insertOne(
{
member: [req.user.id, "admin"],
date: Date.now(),
title: "chat_" + req.body.id,
},
function (req, result) {
console.log("저장완료");
res.send("완료");
}
);
}
});
app.post("/message", function (req, res) {
var saveChat = {
parent: req.body.parent,
userid: req.user._id,
content: req.body.content,
date: new Date(),
};
db.collection("message")
.insertOne(saveChat)
.then((result) => {
res.send(result);
});
});
// app.get("/message/:parentid", function (req, res) {
// res.writeHead(200, {
// Connection: "keep-alive",
// "Content-Type": "text/event-stream",
// "Cache-Control": "no-cache",
// });
// db.collection("message")
// .find({ parent: req.params.parentid })
// .toArray()
// .then((result) => {
// console.log(result);
// res.write("event: test\n");
// res.write(`data: ${JSON.stringify(result)}\n\n`);
// });
// });
io.on("connection", function (socket) {
console.log("연결");
socket.on("joinroom", function (data) {
console.log("입장");
socket.join("room1");
});
socket.on("room1-send", function (data) {
console.log(data);
io.to("room1").emit("broadcast", data);
});
});
app.get("/cklogin", ckLogIn, function (req, res) {
console.log(req.user);
res.send({ user: req.user });
});
function ckLogIn(req, res, next) {
if (req.user) {
next();
} else {
res.send("로그인안하셨는데요?");
}
}
passport.use(
new LocalStrategy(
{
usernameField: "id",
passwordField: "pw",
session: true,
passReqToCallback: false,
},
async function (userId, password, done) {
try {
const user = await db.collection("login").findOne({ id: userId });
if (!user) {
return done(null, false, { reason: "존재하지않는 아이디입니다" });
}
const result = await bcrypt.compare(password, user.pw);
if (result) {
return done(null, user);
}
return done(null, false, { reason: "비밀번호가 틀립니다." });
} catch (e) {
console.log(e);
return done(e);
}
}
)
);
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(async function (inputid, done) {
db.collection("login").findOne({ id: inputid.id }, function (err, result) {
console.log(result);
done(null, result);
});
});
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "/build/index.html"));
});