forked from claudiajs/example-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
39 lines (31 loc) · 1.26 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
/*global require, module*/
var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder(),
Promise = require('bluebird');
module.exports = api;
// set headers as key-value pairs using the success.headers property */
api.get('/hard-coded-headers', function () {
'use strict';
return {a: 'b'};
}, {success: {headers: {'X-Version': '101', 'Content-Type': 'text/plain'}}});
api.get('/error-headers', function () {
'use strict';
throw 'Abort';
}, {error: {headers: {'X-Version': '404'}}});
// or use dynamic headers by returning new api.ApiResponse(content, headers)
api.get('/programmatic-headers', function () {
'use strict';
return new api.ApiResponse('OK', {'X-Version': '202', 'Content-Type': 'text/plain'});
});
// dynamic headers also work with promises, just resolve with new api.ApiResponse
api.get('/programmatic-headers-promise', function () {
'use strict';
return Promise.delay(100).then(function () {
return new api.ApiResponse('OK', {'X-Version': '303', 'Content-Type': 'text/plain'});
});
});
// dynamic headers overwrite default headers,
api.get('/dynamic-over-static', function () {
'use strict';
return new api.ApiResponse('OK', {'X-Version': '303', 'Content-Type': 'text/plain'});
}, {success: {headers: {'X-Version': 101, 'Y-Version': 202}}});