index.js 726 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**!
  2. * Copyright(c) koajs and other contributors.
  3. * MIT Licensed
  4. *
  5. * Authors:
  6. * fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
  7. */
  8. 'use strict';
  9. /**
  10. * Module dependencies.
  11. */
  12. var jsonpBody = require('jsonp-body');
  13. module.exports = jsonp;
  14. function jsonp(app, options) {
  15. options = options || {};
  16. var callback = options.callback || 'callback';
  17. Object.defineProperty(app.context, 'jsonp', {
  18. set: function (obj) {
  19. var jsonpFunction = this.query[callback];
  20. if (!jsonpFunction) {
  21. return this.body = obj;
  22. }
  23. this.set('X-Content-Type-Options', 'nosniff');
  24. this.type = 'js';
  25. this.body = jsonpBody(obj, jsonpFunction, options);
  26. },
  27. configurable: true
  28. });
  29. }