-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
150 lines (141 loc) · 4.92 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
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
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const Joi = require('joi');
const HapiSwagger = require('hapi-swagger');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
const options = {
info: {
'title': 'Test API Documentation',
'version': '0.0.1',
}
};
server.register([
Inert,
Vision,
{
'register': HapiSwagger,
'options': options
}], (err) => {
server.start((err) => {
if (err) {
console.log(err);
} else {
console.log('Server running at:', server.info.uri);
}
// returns sum
server.route({
path: '/api/add',
method: 'POST',
config: {
handler: (request, reply) => {
var sum = parseInt(request.payload.a) + parseInt(request.payload.b);
reply(sum);
},
description: 'Get algebraic sum',
notes: 'Pass two numbers as a & b and returns sum',
tags: ['api'],
validate: {
payload: {
a : Joi.number()
.required(),
b : Joi.number()
.required(),
}
}
}
});
// returns difference
server.route({
path: '/api/diff',
method: 'POST',
config: {
handler: (request, reply) => {
var diff = parseInt(request.payload.a) - parseInt(request.payload.b);
reply(diff);
},
description: 'Get algebraic difference',
notes: 'Pass two numbers as a & b and returns difference',
tags: ['api'],
validate: {
payload: {
a : Joi.number()
.required(),
b : Joi.number()
.required(),
}
}
}
});
// returns product
server.route({
path: '/api/prod',
method: 'POST',
config: {
handler: (request, reply) => {
var prod = parseInt(request.payload.a) * parseInt(request.payload.b);
reply(prod);
},
description: 'Get algebraic product',
notes: 'Pass two numbers as a & b and returns product',
tags: ['api'],
validate: {
payload: {
a : Joi.number()
.required(),
b : Joi.number()
.required(),
}
}
}
});
// returns quotient
server.route({
path: '/api/div',
method: 'POST',
config: {
handler: (request, reply) => {
var div = parseInt(request.payload.a) / parseInt(request.payload.b);
reply(div);
},
description: 'Get algebraic division',
notes: 'Pass two numbers as a & b and returns quotient',
tags: ['api'],
validate: {
payload: {
a : Joi.number()
.required(),
b : Joi.number()
.required(),
}
}
}
});
// returns remainder
server.route({
path: '/api/rem',
method: 'POST',
config: {
handler: (request, reply) => {
var rem = parseInt(request.payload.a) % parseInt(request.payload.b);
reply(rem);
},
description: 'Get algebraic remainder',
notes: 'Pass two numbers as a & b and returns remainder',
tags: ['api'],
validate: {
payload: {
a : Joi.number()
.required(),
b : Joi.number()
.required(),
}
}
}
});
});
});