송용우

Convert Framwork express to koa

...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
27 | friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token | 27 | friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token |
28 | friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None | 28 | friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None |
29 | profile | 유저가 푼 문제 조회 | GET | api/profile/solved:id | 바로가기 | None | 29 | profile | 유저가 푼 문제 조회 | GET | api/profile/solved:id | 바로가기 | None |
30 +| profile | 유저가 푼 문제 동기화 | Update | api/profile/solved:id | 바로가기 | None |
30 | profile | 유저가 푼 문제 개수 조회 | GET | api/profile/solvednum:id | 바로가기 | None | 31 | profile | 유저가 푼 문제 개수 조회 | GET | api/profile/solvednum:id | 바로가기 | None |
31 | profile | 추천 문제 조회 | GET | api/profile/recommendps:id | 바로가기 | None | 32 | profile | 추천 문제 조회 | GET | api/profile/recommendps:id | 바로가기 | None |
32 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/slack | 바로가기 | Jwt Token | 33 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/slack | 바로가기 | Jwt Token |
......
1 -const express = require("express"); 1 +const Koa = require("koa");
2 -const morgan = require("morgan"); 2 +const Router = require("koa-router");
3 +const bodyParser = require("koa-bodyparser");
3 const mongoose = require("mongoose"); 4 const mongoose = require("mongoose");
4 -const app = express();
5 -const bodyParser = require("body-parser");
6 -const api = require("./src/api");
7 const jwtMiddleware = require("./src/lib/jwtMiddleware"); 5 const jwtMiddleware = require("./src/lib/jwtMiddleware");
6 +const app = new Koa();
7 +const router = new Router();
8 require("dotenv").config(); 8 require("dotenv").config();
9 -const { SERVER_PORT, MONGO_URL } = process.env;
10 -app.use(morgan("dev"));
11 -app.use(express.json());
12 -app.use(express.urlencoded({ extended: false }));
13 app.use(bodyParser()); 9 app.use(bodyParser());
14 app.use(jwtMiddleware); 10 app.use(jwtMiddleware);
11 +const api = require("./src/api");
12 +const { SERVER_PORT, MONGO_URL } = process.env;
13 +
14 +router.use("/api", api.routes());
15 +app.use(router.routes()).use(router.allowedMethods());
15 16
16 -app.use("/api", api);
17 mongoose 17 mongoose
18 - .connect(MONGO_URL, { useNewUrlParser: true, useFindAndModify: false }) 18 + .connect(MONGO_URL, {
19 + useNewUrlParser: true,
20 + useFindAndModify: false,
21 + useUnifiedTopology: true,
22 + })
19 .then(() => { 23 .then(() => {
20 console.log("Connected to MongoDB"); 24 console.log("Connected to MongoDB");
21 }) 25 })
......
This diff is collapsed. Click to expand it.
...@@ -8,13 +8,16 @@ ...@@ -8,13 +8,16 @@
8 "bcrypt": "^4.0.1", 8 "bcrypt": "^4.0.1",
9 "body-parser": "^1.19.0", 9 "body-parser": "^1.19.0",
10 "cheerio": "^1.0.0-rc.3", 10 "cheerio": "^1.0.0-rc.3",
11 + "cookie-parser": "^1.4.5",
11 "dotenv": "^8.2.0", 12 "dotenv": "^8.2.0",
12 "eslint-config-prettier": "^6.11.0", 13 "eslint-config-prettier": "^6.11.0",
13 - "express": "^4.17.1",
14 "fs": "^0.0.1-security", 14 "fs": "^0.0.1-security",
15 "iconv": "^3.0.0", 15 "iconv": "^3.0.0",
16 "joi": "^14.3.1", 16 "joi": "^14.3.1",
17 "jsonwebtoken": "^8.5.1", 17 "jsonwebtoken": "^8.5.1",
18 + "koa": "^2.12.0",
19 + "koa-bodyparser": "^4.3.0",
20 + "koa-router": "^9.0.1",
18 "mongoose": "^5.9.17", 21 "mongoose": "^5.9.17",
19 "morgan": "^1.10.0", 22 "morgan": "^1.10.0",
20 "path": "^0.12.7", 23 "path": "^0.12.7",
......
...@@ -12,6 +12,7 @@ exports.register = async (ctx) => { ...@@ -12,6 +12,7 @@ exports.register = async (ctx) => {
12 username: Joi.string().alphanum().min(3).max(20).required(), 12 username: Joi.string().alphanum().min(3).max(20).required(),
13 password: Joi.string().required(), 13 password: Joi.string().required(),
14 }); 14 });
15 +
15 const result = Joi.validate(ctx.request.body, schema); 16 const result = Joi.validate(ctx.request.body, schema);
16 if (result.error) { 17 if (result.error) {
17 ctx.status = 400; 18 ctx.status = 400;
...@@ -33,7 +34,7 @@ exports.register = async (ctx) => { ...@@ -33,7 +34,7 @@ exports.register = async (ctx) => {
33 await user.save(); 34 await user.save();
34 ctx.body = user.serialize(); 35 ctx.body = user.serialize();
35 36
36 - const token = user.generateToekn(); 37 + const token = user.generateToken();
37 ctx.cookies.set("acces_token", token, { 38 ctx.cookies.set("acces_token", token, {
38 //3일동안 유효 39 //3일동안 유효
39 maxAge: 1000 * 60 * 60 * 24 * 3, 40 maxAge: 1000 * 60 * 60 * 24 * 3,
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const router = express.Router(); 2 +const auth = new Router();
3 - 3 +const authCtrl = require("./auth.ctrl");
4 -router.post("/login"); 4 +auth.post("/login", authCtrl.login);
5 -router.get("/logout"); 5 +auth.get("/logout", authCtrl.logout);
6 -router.post("/register"); 6 +auth.post("/register", authCtrl.register);
7 - 7 +
8 - 8 +module.exports = auth;
9 -
10 -
11 -module.exports = router;
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const router = express.Router(); 2 +const friend = new Router();
3 3
4 -router.post("/"); 4 +friend.post("/");
5 -router.delete("/:id"); 5 +friend.delete("/:id");
6 -router.get("/:id"); 6 +friend.get("/:id");
7 -router.get(""); 7 +friend.get("");
8 8
9 -module.exports = router; 9 +module.exports = friend;
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const app = express(); 2 +const api = new Router();
3 3
4 const auth = require("./auth"); 4 const auth = require("./auth");
5 const friend = require("./friend"); 5 const friend = require("./friend");
...@@ -7,10 +7,10 @@ const notify = require("./profile"); ...@@ -7,10 +7,10 @@ const notify = require("./profile");
7 const user = require("./user"); 7 const user = require("./user");
8 const profile = require("./profile"); 8 const profile = require("./profile");
9 9
10 -app.use("/auth", auth); 10 +api.use("/auth", auth.routes());
11 -app.use("/friend", friend); 11 +api.use("/friend", friend.routes());
12 -app.use("/notify", notify); 12 +api.use("/notify", notify.routes());
13 -app.use("/user", user); 13 +api.use("/user", user.routes());
14 -app.use("/profile", profile); 14 +api.use("/profile", profile.routes());
15 15
16 -module.exports = app; 16 +module.exports = api;
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const router = express.Router(); 2 +const notify = new Router();
3 3
4 -router.post("/slack"); 4 +notify.post("/slack");
5 5
6 -module.exports = router; 6 +module.exports = notify;
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const router = express.Router(); 2 +const profile = new Router();
3 3
4 -router.post("/solved:id"); 4 +profile.post("/solved:id");
5 -router.get("/solvednum:id"); 5 +profile.get("/solvednum:id");
6 -router.get("recommendps:id"); 6 +profile.get("recommendps:id");
7 7
8 -module.exports = router; 8 +module.exports = profile;
......
1 -const express = require("express"); 1 +const Router = require("koa-router");
2 -const router = express.Router(); 2 +const user = new Router();
3 +user.post("/");
4 +user.delete("/:id");
5 +user.get("/:id");
6 +user.get("");
3 7
4 -router.post("/"); 8 +module.exports = user;
5 -router.delete("/:id");
6 -router.get("/:id");
7 -router.get("");
8 -
9 -module.exports = router;
......
1 const mongoose = require("mongoose"); 1 const mongoose = require("mongoose");
2 const bcrypt = require("bcrypt"); 2 const bcrypt = require("bcrypt");
3 const jwt = require("jsonwebtoken"); 3 const jwt = require("jsonwebtoken");
4 +
4 const Schema = mongoose.Schema; 5 const Schema = mongoose.Schema;
5 6
6 const UserSchema = new Schema({ 7 const UserSchema = new Schema({
...@@ -12,7 +13,7 @@ UserSchema.methods.setPassword = async function (password) { ...@@ -12,7 +13,7 @@ UserSchema.methods.setPassword = async function (password) {
12 const hash = await bcrypt.hash(password, 10); 13 const hash = await bcrypt.hash(password, 10);
13 this.hashedPassword = hash; 14 this.hashedPassword = hash;
14 }; 15 };
15 -UserSchema.methodss.checkPassword = async function (password) { 16 +UserSchema.methods.checkPassword = async function (password) {
16 const result = await bcrypt.compare(password, this.hashedPassword); 17 const result = await bcrypt.compare(password, this.hashedPassword);
17 return result; 18 return result;
18 }; 19 };
......
This diff is collapsed. Click to expand it.