-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (66 loc) · 1.64 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
const Hapi = require('hapi');
const AJAXCONFIG = require('./src/utils/ajaxConfig');
// 定义服务
const server = Hapi.server({
// 配置端口:3000
port: 3000,
// 配置访问域:localhost
host: 'localhost',
// 配置允许跨域
routes: {
cors: {
origin: ['*']
}
}
});
function router(path, data, method = 'GET') {
return server.route({
path: path,
method: method,
handler() {
return {
code: 200,
success: true,
msg: '',
data: data,
};
}
});
}
const init = async () => {
// 首页“/test”接口测试
router(AJAXCONFIG.TEST, {
name: 'WxCool'
});
// 获取url参数方式一:请求:http://localhost:3000/api/welcome?name=chenxin
server.route({
path: '/api/welcome',
method: 'GET',
handler(request) {
return {
code: 200,
success: true,
data: {
msg: `welcome ${request.query.name}`
}
}
}
});
// 获取url参数方式一:请求:http://localhost:3000/api/welcome/chenxin
server.route({
path: '/api/welcome/{name}',
method: 'GET',
handler(request) {
return {
code: 200,
success: true,
data: {
msg: `${request.params.name},welcome to use hapi!`
}
};
}
});
await server.start();
// console.log(`server running at: ${server.info.uri}`);
};
init();