美文网首页
NestJS(NodeJS)判断请求是否来自移动端

NestJS(NodeJS)判断请求是否来自移动端

作者: Poppy11 | 来源:发表于2021-08-11 11:13 被阅读0次

写个中间件,mobile.middleware.ts

import { HttpException } from "@nestjs/common";

export function judgeMobile(req, res, next) {
  const deviceAgent = req.headers["user-agent"]?.toLowerCase();
  const agentID = deviceAgent?.match(/(iphone|ipod|ipad|android)/);
  if (agentID) {
//移动端
  next();
  } else {
    throw new HttpException("请使用移动端访问", 500)
  }
};

相关文章

网友评论

      本文标题:NestJS(NodeJS)判断请求是否来自移动端

      本文链接:https://www.haomeiwen.com/subject/omaqbltx.html