server-side browser detection? node.js

前端 未结 15 1093
不知归路
不知归路 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: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
    

提交回复
热议问题