用nodejs搭建渠道跳转页面分发统计服务,统计的数据主要在crate索引库中插入。
1、先介绍一下crate库
Crate Data 是一个开源的大规模的可伸缩的数据存储系统,无需任何系统管理需求。提供强大的搜索功能。用于存储各种表格数据、
非结构化数据和二进制对象,并可通过 SQL 进行检索。易于安装和使用,支持高可用性和实时大规模并行访问和处理。Crate 特别适合
用于 Docker 环境中。
2、首先要明白我们需要哪些数据,然后根据要记录的数据来设计索引库表结构,包括记录表和配置表,如图是我设计的表结构
3、然后开始用nodejs开始构建贴出主要的代码loginM.js
var request = require('request');
var url = require('url');
var moment = require('moment');
var Cookies = require('cookies');
var uuidV1 = require('uuid/v1');//通用唯一识别码
module.exports = {
//返回地址参数信息主要用到url和moment模块
logic: function(req, res){
function urlInfo(urlhost){
return {
href: url.parse(req.url).href,
query: url.parse(req.url,true).query,
pathname: url.parse(req.url,true).pathname,
/*time: moment().format('YYYY-MM-DD HH:mm:ss'),*/
time: moment().format('X')
}
}
var resInfo = urlInfo();
return resInfo;
//res.write(resInfo.href+'~~~~'+resInfo.pathname+'~~~~'+resInfo.query.id+'~~~~'+resInfo.query.name+'~~~~'+resInfo.query.desc+'~~~~'+resInfo.time);
},
//获取访问者的ip
getClientIp: function(req){
var ipAddress;
var headers = req.headers;
var forwardedIpsStr = headers['x-real-ip'] || headers['x-forwarded-for'];
forwardedIpsStr ? ipAddress = forwardedIpsStr : ipAddress = null;
if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
},
//判断用户的终端
checkClinetModel: function(req){
var device = '';
function brows($agent){
return {
trident: $agent.indexOf('Trident') > -1,
presto: $agent.indexOf('Presto') > -1,
webKit: $agent.indexOf('AppleWebKit') > -1,
gecko: $agent.indexOf('Gecko') > -1 && $agent.indexOf('KHTML') == -1,
mobile: !!$agent.match(/AppleWebKit.*Mobile.*/),
ios: !!$agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
android: $agent.indexOf('Android') > -1 || $agent.indexOf('Linux') > -1,
iPhone: $agent.indexOf('iPhone') > -1 || $agent.indexOf('Mac') > -1,
iPad: $agent.indexOf('iPad') > -1,
}
}
var deviceAgent=brows(req.headers['user-agent']);
if (deviceAgent.trident){device = 'pc(Trident)';}
if (deviceAgent.presto){device = 'pc(presto)';}
if (deviceAgent.webKit){device = 'pc(webKit)';}
if (deviceAgent.gecko){device = 'pc(gecko)';}
if (deviceAgent.mobile && deviceAgent.ios){device = 'ios';}
if (deviceAgent.mobile && deviceAgent.android){device = 'android';}
if (deviceAgent.mobile && deviceAgent.iPhone){device = 'iPhone';}
if (deviceAgent.mobile && deviceAgent.iPad){device = 'ipad';}
return device;
},
//给用户做标记这里主要用到uuid,然后存到cookie里面,来判断是不是同一个用户
setCookie: function(req, res){
req.cookies = new Cookies(req,res);
var uuid = uuidV1();//生成唯一标识符
var c_uuido = req.cookies.get('uuidInfo');
if(c_uuido == undefined){
req.cookies.set('uuidInfo',uuid,{
maxAge:30 * 24 * 60 * 60 * 60,
});
c_uuido = uuid;
}
return c_uuido;
}
}
3、然后向crate库中插入数据由于crate库能直接以httpkpi的形式,操作表结构所以直接用request模块进行操作
var request = require('request');
var logicM = require('./logic');
module.exports = {
postCrate : function(req, res){
var setCookie = logicM.setCookie(req, res);
var logic = logicM.logic(req, res);
var getClientIp = logicM.getClientIp(req);
var checkClinetModel = logicM.checkClinetModel(req);
var data = {
"name":logic.query.name,
"descs":logic.query.descs,
"href":logic.href,
"pathname":logic.pathname,
"ipaddress":getClientIp,
"uuid":setCookie,
"device":checkClinetModel,
"time":logic.time,
"resinfoid":logic.query.id,
}
request({
url: "crate库的接口地址",
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({ "stmt": "insert into query (name, descs, href , pathname, ipaddress, uuid, device , time , resinfoid) values('"+data.name+"','"+data.descs+"','"+data.href+"','"+data.pathname+"','"+data.ipaddress+"','"+data.uuid+"','"+data.device+"','"+data.time+"','"+data.resinfoid+"') " })
}, function(error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
if(error){
console.log(error);
}else{
console.log('插入数据成功!');
}
}
});
}
}
插入的时候注意两点:
1、插入的时候要严格执行sql语句的语法
2、向crate库中插入数据的时候,要转换成字符串的格式插入