jsonp-body
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

jsonp-body

NPM version Node.js CI Test coverage Known Vulnerabilities npm download Node.js Version PRs Welcome

Helper to create more safe jsonp response body for koa and other web framework.

Install

npm install jsonp-body --save

Usage

const koa = require('koa');
const { jsonp } = require('jsonp-body');

var app = koa();
app.use(async function () {
  this.set('X-Content-Type-Options', 'nosniff');
  if (this.query.callback) {
    this.set('Content-Type', 'text/javascript');
  } else {
    this.set('Content-Type', 'application/json');
  }
  this.body = jsonp({foo: 'bar'}, this.query.callback);
});

API Reference

jsonp(obj, callback, options)

Get obj jsonp string response with callback.

  • obj: object convert to JSON string
  • callback: callback function name
  • options: optional for JSON.stringify
    • limit: length limit for callback, default to 512
    • replacer: replacer in JSON.stringify(obj, [replacer, [space]])
    • space: space in JSON.stringify(obj, [replacer, [space]])

License

MIT

Contributors

Contributors

Made with contributors-img.

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i jsonp-body

    Weekly Downloads

    18,656

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    13.5 kB

    Total Files

    11

    Last publish

    Collaborators

    • fengmk2