-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (24 loc) · 1017 Bytes
/
Copy pathindex.js
File metadata and controls
31 lines (24 loc) · 1017 Bytes
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
'use strict';
const http = require('http');
const port = process.env.PORT || 3000;
const requestHeaderParser = (headers) => {
const softwareRe = /[a-z\s\d\.:;]+(?=\))/i;
const languageRe = /.+(?=,)/;
const ip = headers['x-forwarded-for'];
const ipaddress = ip === undefined ? null : ip.split(',')[0];
const language = languageRe.exec(headers['accept-language'].slice(0))[0];
const software = softwareRe.exec(headers['user-agent'].slice(0))[0];
return { ipaddress, language, software };
};
module.exports.requestHeaderParser = requestHeaderParser;
const server = http.createServer((req, res) => {
const result = requestHeaderParser(req.headers, req.socket);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
});
const startServer = () => server.listen(port, (err) => {
if (err) return console.error(err);
console.log(`server listening on port ${port}`);
});
const args = process.argv.slice(0);
if (args.length > 2 && args[2] === 'start') startServer();