generated from wecode-bootcamp-korea/backend-1st-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
72 lines (50 loc) · 1.68 KB
/
app.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
const http = require("http");
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv").config();
const morgan = require("morgan");
const { DataSource } = require("typeorm");
const jwt = require("jsonwebtoken");
const app = express();
app.use(cors());
app.use(morgan("combined"));
app.use(express.json());
const userServices = require("./services/userServices");
const postServices = require("./services/postServices");
const AppDataSource = new DataSource({
type: process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
});
const { errorHandler } = require("./errorHandler.js");
// 실행
app.get("/", userServices.welcome); // 메인홈
app.get("/users", userServices.getUsers); // 유저데이터 화면
app.post("/users", userServices.createUsers); // 회원가입
app.post("/login", userServices.login); // 로그인
app.post("/posts", postServices.createPosts); // 글 작성
app.get("/posts", postServices.getPost); // 글 목록
const server = http.createServer(app);
// const serverPort = 8000;
const start = async () => {
try {
server.listen(process.env.TYPEORM_SERVERPORT, () =>
console.log(`Server is listening on `, process.env.TYPEORM_SERVERPORT)
);
} catch (err) {
console.error(err);
}
};
AppDataSource.initialize().then(() => {
console.log("Data Source has been initialized!");
});
postServices.AppDataSource.initialize().then(() => {
console.log("Post Source has been initialized!")
});
start();
module.exports = {
AppDataSource,
};