Commit aa59e0a7 authored by Kim, Chaerin's avatar Kim, Chaerin
Browse files

서버 구축 완료

parent 9388ae9e
PG_DATABASE=boradb PG_DATABASE=boraIt
PG_USER=test PG_USER=postgres
PG_PASSWORD=test PG_PASSWORD=postgres
\ No newline at end of file \ No newline at end of file
node_modules/ node_modules/
package-lock.json package-lock.json
\ No newline at end of file config/
env.development
\ No newline at end of file
const endpoints = { const endpoints = {
API_BASE_URL: "https://localhost:8080/api", API_BASE_URL: "https://localhost:8080/api",
HOME_API: "/", HOME_API: "",
ROOM_API: "/room", ROOM_API: "room",
PROFILE_API: "/profile", PROFILE_API: "profile",
}; };
export default endpoints; export default endpoints;
import axios from "axios"; import axios from "axios";
import { API_BASE_URL } from "./endpoints"; import endpoints from "./endpoints";
const login = async (email, password) => { const login = async (email, password) => {
const payload = { email, password }; const payload = { email, password };
const { data } = await axios.post(`${API_BASE_URL}/user`, payload); const { data } = await axios.post(`${endpoints.API_BASE_URL}/`, payload);
return data; return data;
}; };
......
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import login from "../apis/user.api";
const INIT_USER = { const INIT_USER = {
id: "", email: "",
password: "", password: "",
}; };
...@@ -26,7 +27,7 @@ const Login = () => { ...@@ -26,7 +27,7 @@ const Login = () => {
try { try {
// setLoading(true); // setLoading(true);
// setError(""); // setError("");
// const success = await login(user.email, user.password); const success = await login(user.email, user.password);
console.log(user); console.log(user);
setSuccess(success); setSuccess(success);
} catch (error) { } catch (error) {
...@@ -36,7 +37,7 @@ const Login = () => { ...@@ -36,7 +37,7 @@ const Login = () => {
} }
} }
const { id, password } = user; const { email, password } = user;
return ( return (
<div className="modal-content"> <div className="modal-content">
...@@ -57,11 +58,11 @@ const Login = () => { ...@@ -57,11 +58,11 @@ const Login = () => {
<label>아이디</label> <label>아이디</label>
<input <input
className="form-control" className="form-control"
id="id" id="email"
type="text" type="text"
name="id" name="email"
placeholder="아이디를 입력하세요" placeholder="아이디를 입력하세요"
value={id} value={email}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
......
import { Room } from "../models/index.js";
import config from "../config/app.config.js";
const login = async (req, res) => {
try {
console.log("login= ", req.body);
res.json("안녕");
} catch (error) {
console.log(error);
return res.status(500).send("안녕 중 에러");
}
};
export default {
login,
};
import { User } from "../models/index.js";
import config from "../config/app.config.js";
const login = async (req, res) => {
try {
console.log("login= ", req.body);
const { email, password } = req.body;
const user = await User.scope("password").findOne({ where: email });
if (!user)
return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`);
const passworMatch = await user.comparePassword(password);
if (passworMatch) {
const token = jwt.sign({ userID: user.id }, config.jwtSecret, {
expiresIn: config.jwtExpires,
});
res.cookie(config.cookieName, token, {
path: "/",
httpOnly: true,
secure: true,
});
res.json({ user });
} else {
res.status(401).send("비밀번호가 일치하지 않습니다.");
}
} catch (error) {
console.log(error);
return res.status(500).send("로그인 중 에러");
}
};
export default {
login,
};
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import dotenv from "dotenv"; import dotenv from "dotenv";
import app from "./app.js"; import app from "./app.js";
import appConfig from "./config/app.config.js"; import appConfig from "./config/app.config.js";
import { sequelize, User } from "./db/index.js"; import { sequelize, User } from "./models/index.js";
dotenv.config({ dotenv.config({
path: `${ path: `${
...@@ -10,37 +10,59 @@ dotenv.config({ ...@@ -10,37 +10,59 @@ dotenv.config({
}`, }`,
}); });
// production
// sequelize.sync().then(() => {
// app.listen(appConfig.port, () => {
// console.log(`Server is running on port ${appConfig.port}`)
// })
// })
// development
// 주의!!!: {force: true}는 서버가 다시 시작되면 기존 디비 모두 삭제되고 새로운 디비 생성
sequelize sequelize
.sync({ force: true }) .sync({ force: true })
.then(async () => { .then(async () => {
// await Promise.all( console.log(" DB 연결 성공");
// Object.keys(ROLE_NAME).map((name) => { // await User.create({
// return Role.create({ name }); // id: 0,
// }) // name: "admin",
// ); // email: "admin",
// password: "admin!",
// const adminRole = await Role.findOne({ where: { name: "admin" } }); // gender: 0,
// });
await User.create({
name: "admin",
id: "admin",
password: "admin!",
idNumber: "0000",
});
app.listen(appConfig.port, () => { app.listen(appConfig.port, () => {
console.log(`Server is running on port ${appConfig.port}`); console.log(`Server is running on port ${appConfig.port}`);
}); });
}) })
.catch((err) => { .catch((err) => {
console.log("연결 실패");
console.log(err); console.log(err);
}); });
// production
// sequelize.sync().then(() => {
// app.listen(appConfig.port, () => {
// console.log(`Server is running on port ${appConfig.port}`)
// })
// })
// development
// 주의!!!: {force: true}는 서버가 다시 시작되면 기존 디비 모두 삭제되고 새로운 디비 생성
// sequelize
// .sync({ force: true })
// .then(async () => {
// // await Promise.all(
// // Object.keys(ROLE_NAME).map((name) => {
// // return Role.create({ name });
// // })
// // );
// // const adminRole = await Role.findOne({ where: { name: "admin" } });
// await User.create({
// id: "0000",
// name: "admin",
// email: "admin",
// password: "admin!",
// gender: 0,
// });
// app.listen(appConfig.port, () => {
// console.log(`Server is running on port ${appConfig.port}`);
// });
// })
// .catch((err) => {
// console.log(err);
// });
import { Sequelize } from "sequelize";
import UserModel from "./user.model.js";
import RoomModel from "./room.model.js";
const env = process.env.NODE_ENV || "development";
import configs from "../config/config.js";
const config = configs[env];
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
{ host: config.host, dialect: config.dialect }
);
const User = UserModel(sequelize);
const Room = RoomModel(sequelize);
export { sequelize, User, Room };
import bcrypt from "bcryptjs";
import { DataTypes } from "sequelize"; import { DataTypes } from "sequelize";
const RoomModel = (sequelize) => { const RoomModel = (sequelize) => {
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
"description": "Streaming Service", "description": "Streaming Service",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "nodemon server/index.js", "start": "nodemon index.js",
"dev": "nodemon -r esm server/index.js", "dev": "nodemon -r esm index.js",
"build": "tsc -p ." "build": "tsc -p ."
}, },
"repository": { "repository": {
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"pg": "^8.6.0", "pg": "^8.6.0",
"pg-hstore": "^2.3.4", "pg-hstore": "^2.3.4",
"postgres": "^1.0.2",
"sequelize": "^6.6.5", "sequelize": "^6.6.5",
"socket.io": "2.3.0", "socket.io": "2.3.0",
"wrtc": "0.4.6" "wrtc": "0.4.6"
......
import express from "express"; import express from "express";
import userRouter from "./user.route.js"; import userRouter from "./user.route.js";
import roomRouter from "./room.route.js";
const router = express.Router(); const router = express.Router();
router.use("/user", userRouter); router.use("/", userRouter);
router.use("/room/:userId/:channelId", roomRouter);
export default router; export default router;
import express from "express"; import express from "express";
// import authCrtl from "../controllers/auth.controller.js"; import roomCrtl from "../controllers/room.controller.js";
const router = express.Router(); const router = express.Router();
// router.route("/").get(authCrtl.requireLogin); router.route("/").get(roomCrtl.login);
export default router; export default router;
\ No newline at end of file
import express from "express"; import express from "express";
// import authCrtl from "../controllers/auth.controller"; import userCtrl from "../controllers/user.controller.js";
// import userCtrl from "../controllers/user.controller";
const router = express.Router(); const router = express.Router();
// router.route("/").get(authCrtl.requireLogin); router.route("/").post(userCtrl.login);
export default router; export default router;
const config = {
env: process.env.NODE_ENV === 'production' ? 'production' : 'development',
port: process.env.PORT || 8080,
jwtSecret: 'ThisIsSecretKeyOFBoraIt!',
jwtExpires: '1d',
cookieName: 'borabora',
cookieMaxAge: 60 * 60 * 24 * 7 * 1000,
}
export default config
\ No newline at end of file
const config = {
host: 'localhost',
username: process.env.PG_USER || 'test',
password: process.env.PG_PASSWORD || 'test',
database: process.env.PG_DATABASE || 'testdb',
dialect: 'postgres',
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000,
}
}
export default config
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment