-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathweb.js
49 lines (43 loc) · 1.21 KB
/
web.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
/*global require, module*/
var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder(),
fs = require('fs'),
superb = require('superb'),
util = require('util');
module.exports = api;
// just return the result value for synchronous processing
api.get('/hello', function () {
'use strict';
return 'hello world';
});
// pass some arguments using the query string or headers to this
// method and see that they're all in the request object
api.get('/echo', function (request) {
'use strict';
return request;
});
// use request.queryString for query arguments
api.get('/greet', function (request) {
'use strict';
return request.queryString.name + ' is ' + superb.random();
});
// use {} for dynamic path parameters
api.get('/people/{name}', function (request) {
'use strict';
return request.pathParams.name + ' is ' + superb.random();
});
// Return a promise for async processing
api.get('/packagejson', function () {
'use strict';
var read = util.promisify(fs.readFile);
return read('./package.json')
.then(JSON.parse)
.then(function (val) {
return val;
});
});
// use .post to handle a post; or .delete, .patch, .head, .put
api.post('/echo', function (request) {
'use strict';
return request;
});