-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (87 loc) · 2.67 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var PORT = 9889;
var http = require('http');
function jsonSplit(url) {
var jsonpReg = /jsonp.js/;
if (!jsonpReg.test(url)) {
return {};
}
var propsArr = url.split('?');
// 如果请求头并不包含回调函数名,请求参数则返回空对象
if (!propsArr[1]) {
return {};
}
var properties = propsArr[1].split('&');
var resultProps = properties.map(function(item) {
return item.split('=');
})
var result = {};
for (var i = 0, len = resultProps.length; i < len; i++) {
result[resultProps[i][0]] = resultProps[i][1];
}
return result;
}
// 模拟查询数据库
function querySQL(info) {
return info + Math.floor(Math.random() * 100);
}
// 产生jsonp返回的结果
function generateResponseData(obj) {
var result = {};
for (var i in obj) {
if (i !== 'callback') {
result[i] = querySQL(obj[i]);
}
}
return JSON.stringify(result);
}
var server = http.createServer(function(request, response) {
// 获取想要访问的url
var url = request.url;
console.log(url);
console.log(/jsonp.js/.test(url));
// 获取请求的地址
var referer = request.headers.referer;
// ajax 处理,当访问/xhr目录时,视为ajax请求
var xhrReg = /xhr/;
var jsonpReg = /jsonp.js/;
if (xhrReg.test(url)) {
console.log('xhr request received');
var accessDomain = '*';
response.writeHead(200, {
'Content-Type': 'text/plain',
// 参考http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work
// 解决跨域权限问题,结果为'*'会通知浏览器,该此返回的数据运行所有的网站访问
// 同样的如果将其设置为 http://www.forclan.com则只允许forclan域名下的页面访问
// 相当于返回的数据由浏览器视为与’accessDomain‘同域
'Access-Control-Allow-Origin': accessDomain,
});
// put your CORS results here
var obj = {
name: 'forclan',
age: 1080,
port: PORT,
requestReferer: referer
};
response.end(JSON.stringify(obj));
}
else if (jsonpReg.test(url)) {
console.log('jsonp request received');
// 将接收到到查询语句转换为json对象
var receiveObj = jsonSplit(url);
// 从json对象中分离
var responseData = generateResponseData(receiveObj);
response.writeHead(200, {
'Content-Type': 'text/javascript',
})
var callbackName = receiveObj.callback || '';
response.end(callbackName + '(' + responseData + ');');
}
else {
response.writeHead(200, {
'Content-Type': 'text/plain'
})
response.end('not found');
}
});
server.listen(PORT);
console.log('Server runing at port: ' + PORT + '.');