server-side browser detection? node.js

前端 未结 15 1085
不知归路
不知归路 2020-12-07 09:29

Most implementations i\'ve seen are for browser detection on the client side. I was just wondering if it was possible to do browser detection before sending any resources to

相关标签:
15条回答
  • 2020-12-07 10:04

    Most browsers provide an HTTP request header called "User-Agent" This is the same as the navigator.userAgent property on the client side.

    0 讨论(0)
  • 2020-12-07 10:05

    The ua-parser library for node (npm install ua-parser) exposes a big set of regexes for browser user-agent strings. I'd strongly recommend it for your needs.

    0 讨论(0)
  • 2020-12-07 10:06

    I wanted to do a simple redirection to a mobile version of my site, so user-agent is reliable enough. I wanted to do it server-side so I didn't waste time loading unnecessary css and js on the client. http://detectmobilebrowsers.com/ had the most robust regex to match. So I threw together some express middleware that will let you do the redirection by just adding two lines of code to your app.

    npm install detectmobilebrowsers to install

    express = require 'express'
    mobile  = require 'detectmobilebrowsers'
    
    app = express()
    app.configure () ->
      app.use mobile.redirect 'http://m.domain.com'
    app.get '/', (req, res) ->
      res.send 'Not on Mobile'
    app.listen 3000
    
    0 讨论(0)
提交回复
热议问题