-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
92 lines (80 loc) · 2.49 KB
/
route.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
// Copyright 2014 FOCUS Inc.All Rights Reserved.
/**
* @fileOverview Probe
* @author sheny@made-in-china.com
*/
var urlStatic = require('./urlStatic'),
path = require('path'),
queryString = require('querystring'),
url = require('url'),
log = require('./modules/log'),
filter;
function route(pathName, request, response) {
var mapping = getMapping(pathName),
isAjax = (request.headers['x-requested-with'] === 'XMLHttpRequest');
try {
if (!mapping) {
if (isAjax) {
response.writeHead(404, {
"Content-Type" : 'text/html'
})
} else {
response.writeHead(302, {
Location : '/error/404.html'
});
}
response.end();
} else {
parameter(request, function(params) {
request['params'] = params || {};
response.writeHead(200, {"Content-Type":'text/html;charset:utf-8'});
filter = require('.'+ mapping.server);
filter.renderPage(request, response, path.join(__dirname, (mapping.render ? mapping.render : '')), function(content) {
response.write(content);
response.end();
});
});
}
} catch(err) {
log.info('500 ERROR:'+ err);
if (isAjax) {
response.writeHead(500, {
"Content-Type" : 'text/html'
});
response.write(err.toString());
} else {
response.writeHead(302, {
Location : '/error/500.html'
});
}
response.end();
}
}
function getMapping(pathName) {
if (!pathName) return null;
if (urlStatic[pathName]) {
return urlStatic[pathName];
}
for (var path in urlStatic) {
if (urlStatic[path].reg) {
if (new RegExp(urlStatic[path].reg).test(pathName)) {
return urlStatic[path];
}
}
}
return null;
}
function parameter(request, callback) {
if (request.method.toLowerCase() === 'post') {
var receiveData = '';
request.setEncoding("utf8");
request.addListener("data", function (postDataChunk) {
receiveData += postDataChunk;
}).addListener('end', function() {
callback(queryString.parse(receiveData));
});
} else {
callback(url.parse(request.url, true).query);
}
}
module.exports = route;