-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.51 KB
/
index.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
import express from "express";
import env from "dotenv";
import bodyParser from "body-parser";
import Logging from "./logging/Logging.js";
import { apiRoutes } from "./routes/weather.js";
import { conn, run } from "./v1/functions/tomongo.js";
let app = express();
const L = new Logging();
env.config();
await conn()
.then(() => {
L.info("connection established");
})
.catch((err) => {
L.error("failed to connect");
L.error(`${err.message}`);
});
await run();
const startApp = () => {
app.use((req, res, next) => {
L.info(` Incoming Request -> Method: [${req.method}] - URL: [${req.url}] - IP: [${req.socket.remoteAddress}]`);
res.on("finish", () => {
L.info(` Incoming Request Finish -> Method: [${req.method}] - URL: [${req.url}] - IP: [${req.socket.remoteAddress}] - StatusCode: [${res.statusCode}] - Status: [${res.statusMessage}]`);
});
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", (req, res, next) => {
res.send({ message: "Worked" });
});
app.use("/api", apiRoutes);
app.use((req, res, next) => {
L.info(req.route);
if (!req.route) {
res.status(404).json({ message: "endpoint tidak ditemukan" });
} else {
next();
}
});
app.listen(process.env.PORT, () => {
L.info(`listening on ${process.env.PORT}`);
});
};
startApp();