forked from doowb/request-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (98 loc) · 3.07 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* request-info <https://github.com/doowb/request-info>
*
* Copyright (c) 2016-present, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
const UserAgentParser = require('ua-parser-js');
/**
* Extract the useragent information
* and return as an object.
* @param {Object} `req` http request object (from http or express)
* @return {Object} useragent information parsed from `user-agent` header
*/
const parseUserAgent = req => {
var ua = new UserAgentParser();
ua.setUA(header(req, 'user-agent'));
return ua.getResult();
};
/**
* Get the ip address from the request object
* @param {Object} `req` http request object (from http or express)
* @param {String} `def` default ip address if one isn't found
* @return {String} ip address
*/
const ip = (req, def) => {
return req.ip
|| req.connection?.remoteAddress
|| req.socket?.remoteAddress
|| req.socket?.socket?.remoteAddresss
|| def;
};
/**
* Get the header from either the `header` function on `req` or from the `headers` array.
*
* ```js
* var referer = header(req, 'referer', '<undefined>');
* console.log(referer);
* //=> 'http://localhost'
* ```
* @param {Object} `req` Request object from http or [express]
* @param {String} `prop` Header property to get.
* @param {String} `def` Default to return if the property is not found.
* @return {String} Specified header or default if the header is not found.
*/
const header = (req, prop, def) => {
if (typeof req.header === 'function') {
return req.header(prop) || def;
}
return req.headers[prop] || def;
};
/**
* Get information information about the given http request.
*
* ```js
* const express = require('express');
* const app = express();
*
* app.get('/', (req, res) => {
* console.log(info(req));
* // {
* // httpVersion: '1.1',
* // ip: '127.0.0.1',
* // method: 'GET',
* // referer: 'http://localhost:8080/index.html',
* // url: '/',
* // ua: {
* // ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
* // browser: { name: 'Chrome', version: '52.0.2743.116', major: '52' },
* // engine: { version: '537.36', name: 'WebKit' },
* // os: { name: 'Mac OS', version: '10.9.5' },
* // device: { model: undefined, vendor: undefined, type: undefined },
* // cpu: { architecture: undefined }
* // }
* // }
* });
* ```
* @param {Object} `req` http request object (from http or express)
* @return {Object} info object containing `httpVersion`, `ip`, `method`, `referer`, `url`, and `ua` (useragent information)
* @api public
*/
module.exports = (req = {}) => {
try {
const { httpVersionMajor, httpVersionMinor, method, url = '<undefined>' } = req;
const data = parseUserAgent(req);
const info = {
...data,
method,
ip: ip(req, '127.0.0.1'),
url,
httpVersion: `${httpVersionMajor}.${httpVersionMinor}`,
referer: header(req, 'referer', '<undefined>')
};
return info;
} catch (err) {
console.log(err);
}
};