-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexRouter.ts
42 lines (36 loc) · 1.21 KB
/
indexRouter.ts
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
import * as dotenv from "dotenv";
dotenv.config();
import express, { Router, Request, Response } from "express";
import axios from "axios";
import moment from "moment";
const url_api = process.env.API_URL as string;
// console.log({ url_api });
const fetchDate = async () => {
let dateToSend;
await axios
.get(url_api)
.then((response) => {
dateToSend = response?.data[0]?.commit?.author?.date;
// console.log({ dateToSend });
})
.catch((error) => {
console.log({ error });
});
if (dateToSend) {
dateToSend = new Date(dateToSend);
dateToSend = moment(dateToSend).fromNow();
return dateToSend;
}
};
const indexRouter: Router = express.Router({ strict: false, caseSensitive: true });
indexRouter.get("/", async (req: Request, res: Response) => {
await console.log("req.ip:", req.ip);
const lastCommitDate = await fetchDate();
// await console.log("lastCommitDate:", lastCommitDate);
await res.render("pages/index", { lastCommitDate: lastCommitDate });
});
indexRouter.get("/*", async (_req: Request, res: Response) => {
const lastCommitDate = await fetchDate();
await res.render("pages/notFound", { lastCommitDate: lastCommitDate });
});
export default indexRouter;