πŸ“¦ SeolJaeHyeok / My-shopping-mall

πŸ“„ app.js Β· 48 lines
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
48import cors from "cors";
import express from "express";
import {
  viewsRouter,
  userRouter,
  orderRouter,
  productRouter,
  adminRouter,
  categoryRouter,
} from "./routers";
import { errorHandler } from "./middlewares";
import dotenv from "dotenv";
import morgan from "morgan";
import { accessLogStream } from "./config/log";

const app = express();
dotenv.config();

// CORS μ—λŸ¬ λ°©μ§€
app.use(cors());

// Content-Type: application/json ν˜•νƒœμ˜ 데이터λ₯Ό μΈμ‹ν•˜κ³  핸듀링할 수 있게 함.
app.use(express.json());

// Content-Type: application/x-www-form-urlencoded ν˜•νƒœμ˜ 데이터λ₯Ό μΈμ‹ν•˜κ³  핸듀링할 수 있게 함.
app.use(express.urlencoded({ extended: false }));
app.use(morgan("dev"));
app.use(
  morgan(":date[iso] :method :status :response-time :url :referrer", {
    stream: accessLogStream,
  })
);
// html, css, js λΌμš°νŒ…
app.use(viewsRouter);
// api λΌμš°νŒ…
// μ•„λž˜μ²˜λŸΌ ν•˜λ©΄, userRouter μ—μ„œ '/login' 으둜 λ§Œλ“  것이 μ‹€μ œλ‘œλŠ” μ•žμ— /apiκ°€ λΆ™μ–΄μ„œ
// /api/login 으둜 μš”μ²­μ„ ν•΄μ•Ό ν•˜κ²Œ 됨. λ°±μ—”λ“œμš© λΌμš°νŒ…μ„ κ΅¬λΆ„ν•˜κΈ° μœ„ν•¨μž„.
app.use("/api/admin", adminRouter);
app.use("/api", userRouter);
app.use("/api/order", orderRouter);
app.use("/api/product", productRouter);
app.use("/api/category", categoryRouter);
// μˆœμ„œ μ€‘μš” (errorHandler은 λ‹€λ₯Έ 일반 λΌμš°νŒ…λ³΄λ‹€ λ‚˜μ€‘μ— μžˆμ–΄μ•Ό 함)
// κ·Έλž˜μ•Ό, μ—λŸ¬κ°€ 났을 λ•Œ next(error) ν–ˆμ„ λ•Œ μ—¬κΈ°λ‘œ 였게 됨
app.use(errorHandler);

export { app };