-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (66 loc) · 1.81 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require("fs");
const express = require("express");
const marked = require("marked");
const app = express();
// Home page
app.get("/", (req, res) => {
fs.readFile("./README.md", "utf8", (err, content) => {
if (err) return res.status(500);
marked(content, (err, parsed) => {
if (err) return res.status(500);
const page = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<title>Free Code Camp Timestamp Microservice project</title>
<style>body {font: 1em "Open Sans"; margin: 40px}</style>
</head>
<body>
<main>
<article>${parsed}</article>
</main>
</body>
</html>
`;
return res.send(page);
});
});
});
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
app.get("/:date", (req, res) => {
let result = { unix: null, natural: null };
// create Date object
const date = isTimestamp(req.params.date)
? new Date(+req.params.date)
: new Date(req.params.date);
// if date is incorrect
if (isNaN(date)) return res.send(result);
// filled the result object and send it
result.unix = date.getTime();
result.natural = `${monthNames[date.getMonth()]} ${(
"0" + date.getDate()
).slice(-2)}, ${date.getFullYear()}`;
return res.send(result);
});
// helper function
function isTimestamp(ts) {
return !isNaN(ts) && isFinite(ts) && /\d{8,15}/.test(ts);
}
app.listen(process.env.PORT || 8080);