我有一个看起来像这样的日志:
GET /nands-adblock-logs/a.png HTTP/1.1 http://www.ok.co.uk/celebrity-news/exclusive-stephanie-davis-jeremy-mcconnell-moving-in-together-marriage Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1
我的代码看起来像这样解析数据并返回用户代理:
var fs = require('fs'),
readline = require('readline'),
useragent = require('express-useragent');
function processFile(inputFile) {
console.log("INPUT FILE", inputFile);
instream = fs.createReadStream(inputFile),
outstream = new (require('stream'))(),
rl = readline.createInterface(instream, outstream);
rl.on('line', function (line) {
var ua = useragent.parse(line);
console.log("PARSED USER AGENT = ", ua);
});
rl.on('close', function (line) {
console.log(line);
console.log('done reading file.');
});
}
processFile(process.argv[2]);
输出如下:
PARSED USER AGENT = { isMobile: true,
isTablet: false,
isiPad: false,
isiPod: false,
isiPhone: false,
isAndroid: true,
isBlackberry: false,
isOpera: false,
isIE: false,
isEdge: false,
isIECompatibilityMode: false,
isSafari: false,
isFirefox: false,
isWebkit: false,
isChrome: true,
isKonqueror: false,
isOmniWeb: false,
isSeaMonkey: false,
isFlock: false,
isAmaya: false,
isEpiphany: false,
isDesktop: false,
isWindows: false,
isLinux: true,
isLinux64: false,
isMac: false,
isChromeOS: false,
isBada: false,
isSamsung: false,
isRaspberry: false,
isBot: false,
isCurl: false,
isAndroidTablet: false,
isWinJs: false,
isKindleFire: false,
isSilk: false,
isCaptive: false,
isSmartTV: false,
silkAccelerated: false,
browser: 'Chrome',
version: '42.0.2311.133',
os: 'Linux',
platform: 'Android',
geoIp: {},
source: '"GET /nands-adblock-logs/a.png HTTP/1.1","http://www.ok.co.uk/","Mozilla/5.0 (Linux; Android 5.1.1; D2303 Build/18.6.A.0.175) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/42.0.2311.133 Mobile Safari/537.36"' }
我想要的只是返回以下属性:
DEVICE_TYPE, 浏览器, 操作系统
如何检索这些值?我并不那么担心返回错误的值。
答案 0 :(得分:1)
我会使用lodash:
_.pick(data, function(value, key) {
return key === 'browser' || key === 'os' || value === true;
})
var result = _.pick(data, function(value, key) {
return key === 'browser' || key === 'os' || value === true;
});
document.write('<pre>' + JSON.stringify(result, null, 4) + '</pre>')
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script>
<script>
var data = {
isMobile: true,
isTablet: false,
isiPad: false,
isiPod: false,
isiPhone: false,
isAndroid: true,
isBlackberry: false,
isOpera: false,
isIE: false,
isEdge: false,
isIECompatibilityMode: false,
isSafari: false,
isFirefox: false,
isWebkit: false,
isChrome: true,
isKonqueror: false,
isOmniWeb: false,
isSeaMonkey: false,
isFlock: false,
isAmaya: false,
isEpiphany: false,
isDesktop: false,
isWindows: false,
isLinux: true,
isLinux64: false,
isMac: false,
isChromeOS: false,
isBada: false,
isSamsung: false,
isRaspberry: false,
isBot: false,
isCurl: false,
isAndroidTablet: false,
isWinJs: false,
isKindleFire: false,
isSilk: false,
isCaptive: false,
isSmartTV: false,
silkAccelerated: false,
browser: 'Chrome',
version: '42.0.2311.133',
os: 'Linux',
platform: 'Android',
geoIp: {},
source: '"GET /nands-adblock-logs/a.png HTTP/1.1","http://www.ok.co.uk/","Mozilla/5.0 (Linux; Android 5.1.1; D2303 Build/18.6.A.0.175) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/42.0.2311.133 Mobile Safari/537.36"'
};
</script>
使用最新版本,您必须将pick
替换为pickBy
,但plnkr只有3.10版本。