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

2021-08-11  本文已影响0人  Poppy11

写个中间件,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)
  }
};
上一篇下一篇

猜你喜欢

热点阅读