后端识别PC/移动端
通过user-agent
来判断是移动端,还是pc端。
判断函数是通过关键字来正则匹配判断:
function isMobile (agent) {
return /(iphone|ipod|ipad|android)/.test(agent.toLowerCase())
}
github的DEMO链接,请clone或者fork下来查看(顺手给个star呗):
https://github.com/qq20004604/same_url_different_file
核心点是:routes/index.js
这个文件,下面这段逻辑:
if (isMobile(req.headers['user-agent'])) {
title = '移动端'
} else {
title = 'PC端'
}
通过user-agent
来判断是移动端,还是pc端。
判断函数是通过关键字来正则匹配判断:
function isMobile (agent) {
return /(iphone|ipod|ipad|android)/.test(agent.toLowerCase())
}
访问链接:http://127.0.0.1:3000/
PC端访问的话,title是【PC端】;
移动端访问,title则是【移动端】;
还没有评论,来说两句吧...