-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (129 loc) · 4.29 KB
/
index.js
File metadata and controls
151 lines (129 loc) · 4.29 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const Hapi = require('hapi');
const QRCode = require('qrcode');
const md5 = require('md5');
const fs = require('fs');
const Mock = require('mockjs');
const config = require('./config.json');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: config.port
});
server.register(require('inert'), (err) => {
if (err) {
throw err;
}
// qrcode generator
server.route({
method: 'GET',
path: '/qrcode/{url}',
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
handler: function(request, reply) {
const url = request.params.url,
md5Url = md5(url),
path = config.qrcode + md5Url + '.jpg';
fs.exists(path, function(exists) {
if (exists) {
reply.file(path);
} else {
QRCode.save(path, url, {
errorCorrectLevel: 'high'
}, function(err) {
if (err) {
throw err;
}
reply.file(path);
});
}
});
}
});
server.route({
method: 'GET',
path: '/jsonp',
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
handler: function(request, reply) {
var params = request.query,
json = {
code: 1,
msg: '参数错误'
},
err = {
code: -1,
msg: '系统错误'
};
if (!params.callback) {
params.callback = 'callback';
}
// static data file
if (params.filename) {
if (/^[a-zA-Z]+[a-zA-Z\d\-\_\.\/]+\.json$/.test(params.filename)) {
console.log(config.datas + params.filename);
json = fs.readFileSync(config.datas + params.filename).toString();
} else if (/^[a-zA-Z]+[a-zA-Z\d\-\_\.\/]+\.csv$/.test(params.filename)) {
json = (fs.readFileSync(config.datas + params.filename)).toString();
// replace all
json = json.replace(/\"/g, '\\"');
json = json.replace(/[\r\n]+/g, '\\n');
json = '"' + json + '"';
}
}
if (typeof json === 'object') {
json = JSON.stringify(json);
}
reply(params.callback + '(' + json + ');').type('text/javascript');
}
});
// mock data
server.route({
method: 'GET',
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
path: '/mock/{template}',
handler: function(request, reply) {
const template = request.params.template;
const data = Mock.mock(JSON.parse(template));
reply(JSON.stringify(data)).type('application/json');
}
});
server.route({
method: 'POST',
path: '/mock',
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
handler: function(request, reply) {
const params = request.payload;
if (params && params.template) {
const data = Mock.mock(JSON.parse(params.template));
reply(JSON.stringify(data)).type('application/json').header('Access-Control-Allow-Origin', '*');
} else {
reply('{code: 0, msg: "参数错误"}').type('application/json').header('Access-Control-Allow-Origin', '*');
}
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});