-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (41 loc) · 1.33 KB
/
index.js
File metadata and controls
48 lines (41 loc) · 1.33 KB
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
const express = require("express");
const app = express();
function getInfo(req) {
const ua = req.headers["user-agent"];
return {
ipaddress: req.headers["x-forwarded-for"] || req.ip,
language: req.acceptsLanguages()[0],
software: ua.slice(ua.indexOf("(") + 1, ua.indexOf(")"))
};
}
// home page
app.get("/", (req, res) => {
const clientData = getInfo(req);
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 Request Header Parser Microservice</title>
<style>body {font: 1em "Open Sans"; margin: 40px}</style>
</head>
<body>
<h2>Free Code Camp :: Request Header Parser Microservice</h2>
<hr>
<ul>
<li>Your IP Address is <em>${clientData.ipaddress}</em></li>
<li>Your language is <em>${clientData.language}</em></li>
<li>Your software is <em>${clientData.software}</em></li>
</ul>
<hr>
<a href="/whoami">Get JSON</a>
</body>
</html>
`;
res.send(page);
});
app.get("/whoami", (req, res) => res.send(getInfo(req)));
app.listen(process.env.PORT || 3000);