Node Js-识别请求是来自移动设备还是非移动设备

问题描述 投票:0回答:1

我对node js还是很陌生。是否有任何变通办法或方法来识别来自客户端的使用Node JS来自移动设备还是非移动设备的请求?因为我现在要做的是根据设备类型(移动/台式机)限制对某些API的访问。我在服务器端使用restify。谢谢。

javascript node.js restify
1个回答
0
投票

@ H.Mustafa,一种检测客户端是否正在使用移动设备的基本方法是通过匹配userAgent中的特定字符串集。

function detectMob() {
    const toMatch = [
        /Android/i,
        /webOS/i,
        /iPhone/i,
        /iPad/i,
        /iPod/i,
        /BlackBerry/i,
        /Windows Phone/i
    ];

    return toMatch.some((toMatchItem) => {
        return navigator.userAgent.match(toMatchItem);
    });
}

(参考:Detecting a mobile browser

在客户端设备中运行此代码段。如果返回的结果为true,则说明它是移动设备,否则是台式机/笔记本电脑。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.