forked from wecode-bootcamp-korea/43-1st-4iz-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (37 loc) · 1.06 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
// built-in package
// 3rd-party package
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
// custom package
const route = require("./api/routes");
const dataSource = require("./api/models/dataSource");
const { globalErrorHandler } = require("./api/utils/error");
dataSource
.initialize()
.then(() => {
console.log("Data Source has been initialized!");
})
.catch((error) => {
console.error("Error during Data Source initialization", error);
});
const app = express();
app.use(cors());
app.use(morgan("combined"));
app.use(express.json());
app.use(route);
app.all("*", (req, res, next) => {
const err = new Error(`Can't find ${req.originalUrl} on this server!`);
err.statusCode = 404;
next(err);
});
app.use(globalErrorHandler);
app.get("/ping", async (req, res) => {
return res.status(200).json({ message: "pong!" });
});
const HOST = process.env.HOST;
const PORT = process.env.PORT;
app.listen(PORT, HOST, () => {
console.log(`Server is listening on ${HOST}:${PORT}`);
});