Skip to content

Instantly share code, notes, and snippets.

@justengland
Created April 26, 2016 05:59
Show Gist options
  • Save justengland/3142ecc51775b7b9b71481c9609c8c0a to your computer and use it in GitHub Desktop.
Save justengland/3142ecc51775b7b9b71481c9609c8c0a to your computer and use it in GitHub Desktop.
/*global require, module*/
const ApiBuilder = require('claudia-api-builder');
const api = new ApiBuilder();
module.exports = api;
api.corsHeaders('Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Api-Version');
/* define a global function that returns an allowed cors origin. this will be used for the OPTIONS access-control-allow-origin response header */
api.corsOrigin(function (request) {
'use strict';
if (/claudiajs.com$/.test(request.headers.Origin)) {
return request.headers.Origin;
}
return '';
});
// cors headers will automatically be added to all requests
api.get('/echo', function (request) {
'use strict';
return request;
});
// you can customise individual responses as well
api.get('/programmatic-headers', function () {
'use strict';
return new api.ApiResponse('OK', {'Access-Control-Allow-Origin': 'https://www.claudiajs.com'});
}, {success: {headers: ['Access-Control-Allow-Origin']}});
api.get('/hello', function (request) {
'use strict';
return { hello: 'claudia' };
});
api.get('/get-time', function (request) {
'use strict';
return { time: new Date() };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment