{"id":13798766,"url":"https://github.com/kilianc/koa-jsonp","last_synced_at":"2025-10-18T12:53:38.529Z","repository":{"id":12761256,"uuid":"15434563","full_name":"kilianc/koa-jsonp","owner":"kilianc","description":"Koajs JSONP streaming friendly middleware with GET/POST support","archived":false,"fork":false,"pushed_at":"2018-09-14T08:26:51.000Z","size":23,"stargazers_count":31,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-25T07:41:28.618Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kilianc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-25T12:05:57.000Z","updated_at":"2021-08-10T10:56:30.000Z","dependencies_parsed_at":"2022-09-14T11:00:59.736Z","dependency_job_id":null,"html_url":"https://github.com/kilianc/koa-jsonp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kilianc%2Fkoa-jsonp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kilianc%2Fkoa-jsonp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kilianc%2Fkoa-jsonp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kilianc%2Fkoa-jsonp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kilianc","download_url":"https://codeload.github.com/kilianc/koa-jsonp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248381789,"owners_count":21094528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T00:00:52.765Z","updated_at":"2025-10-18T12:53:38.424Z","avatar_url":"https://github.com/kilianc.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"# koa-jsonp\n\n[![js-standard-style](https://img.shields.io/badge/Code%20Style-Standard-green.svg?style=flat-square)](https://github.com/feross/standard)\n[![Travis CI](https://img.shields.io/travis/kilianc/koa-jsonp.svg?style=flat-square)](https://travis-ci.org/kilianc/koa-jsonp)\n[![NPM version](https://img.shields.io/npm/v/koa-jsonp.svg?style=flat-square)](https://www.npmjs.com/package/koa-jsonp)\n[![NPM downloads](https://img.shields.io/npm/dm/koa-jsonp.svg?style=flat-square)](https://www.npmjs.com/package/koa-jsonp)\n[![GitHub Stars](https://img.shields.io/github/stars/kilianc/koa-jsonp.svg?style=flat-square)](https://github.com/kilianc/koa-jsonp)\n[![License](https://img.shields.io/npm/l/koa-jsonp.svg?style=flat-square)](https://www.npmjs.com/package/koa-jsonp)\n\nA [koajs](https://github.com/koajs/koa) streaming friendly JSONP middleware that supports GET/POST JSONP requests.\n\n## Install\n    $ npm install koa-jsonp\n\n## Example\n\n```js\nconst db = require('nano')('http://localhost:5984/my_db')\nconst stringify = require('json-array-stream')\nconst jsonp = require('koa-jsonp')\n\napp.use(jsonp())\n\napp.use(mount('/users', function *() {\n  this.type = 'json'\n  this.body = db.view('koa_example', 'users')\n    .pipe(JSONStream.parse('rows.*.value'))\n    .pipe(stringify())\n}))\n\napp.use(mount('/dow', function *() {\n  this.body = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']\n}))\n\napp.listen(8080)\n```\n\nyield\n\n    GET  http://localhot:8080/users                       -\u003e JSON\n    POST http://localhot:8080/users                       -\u003e JSON\n    GET  http://localhot:8080/users?callback=onUserList   -\u003e JSONP\n    POST http://localhot:8080/users?callback=onUserList   -\u003e JSONP + iFrame\n\n    GET  http://localhot:8080/dow                         -\u003e JSON\n    POST http://localhot:8080/dow                         -\u003e JSON\n    GET  http://localhot:8080/dow?callback=dowReady       -\u003e JSONP\n    POST http://localhot:8080/dow?callback=dowReady       -\u003e JSONP + iFrame\n\n## API\n\n```js\nfunction jsonp (options)\n```\n\nReturns the generator middleware.\n\n### Options\n* **domain** - (`String`: defaults to `'.default.lan'`) the first level domain where your API will be consumed. Used in iframe mode ([???](#cors))\n* **callbackName**: (`String`: defaults to `'callback'`) The name of the JSONP callback\n\n### Use cases\n\n* __JSONP__: `text/javascript`\n* __JSONP + iFrame__: `text/html`\n\nThe middleware auto selects the right format based on the presence of callback (querystring) and the HTTP method.\n\n![table](http://f.cl.ly/items/460B2P0h3m3c22000W1p/json-transport-0.1.0.png)\n\n# JSONP CORS\n\n*This is not required if your frontend and your API run in the same domain or if you set the `Access-Control-*` headers properly. In this case just use an `XMLHttpRequest` and don't use this middleware at all. Reference: [MDN](https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS) - [HTML5Rocks](http://www.html5rocks.com/en/tutorials/cors/) - [SO](http://stackoverflow.com/questions/13146892/cors-access-control-allow-headers-wildcard-being-ignored)*\n\nThere is a way to read a JSONP response after sending a POST request (form) in the browser. The solution is to set an iframe as target of the form (could be hidden). The iframe will load your response page, in this case plain HTML with a script tag and will call your `callback` using `parent.callbackname(data)`.\n\nThis is an old snippet using `mootools` that creates a form, an iframe, and posts your request. Then dispose everything after executing your callback.\nYou can easily port it to jQuery.\n\n```js\nvar __requestsMap__ = {}\n\nfunction post(url, fields, callback) {\n  var requestId = new Date().getTime()\n  var inputs = []\n\n  Object.keys(fields).forEach(function (fieldName) {\n    inputs.push(new Element('input', {\n      name: fieldName,\n      value: data[fieldName]\n    }))\n  })\n\n  if (url.match(/\\?/)) {\n    url += '\u0026callback=__requestsMap__[\"' + requestId + '\"]'\n  } else {\n    url += '?callback=__requestsMap__[\"' + requestId + '\"]'\n  }\n\n  var form = new Element('form', {\n    'enctype': enctype,\n    'method': 'post',\n    'action': url,\n    'target': requestId,\n    'style': 'display: none'\n  }).adopt(inputs).inject(document.body)\n\n  var iframe = new Element('iframe', {\n    id: requestId,\n    name: requestId,\n    styles: { display: 'none' }\n  }).inject(document.body)\n\n  __requestsMap__[requestId] = function (response) {\n    callback(null, response)\n    // cleanup\n    delete __requestsMap__[requestId]\n    iframe.dispose().destroy()\n    form.dispose().destroy()\n  }\n\n  form.submit()\n}\n```\n\n# How to contribute\n\nkoa-jsonp follows (more or less) the [Felix's Node.js Style Guide](http://nodeguide.com/style.html), your contribution must be consistent with this style.\n\nThe test suite is written on top of [mochajs/mocha](http://mochajs.org) and it took hours of hard work. Please use the tests to check if your contribution is breaking some part of the library and add new tests for each new feature.\n\n    ⚡ npm test\n\nand for your test coverage\n\n    ⚡ make test-cov\n\n## License\n\n_This software is released under the MIT license cited below_.\n\n    Copyright (c) 2013 Kilian Ciuffolo, me@nailik.org. All Rights Reserved.\n\n    Permission is hereby granted, free of charge, to any person\n    obtaining a copy of this software and associated documentation\n    files (the 'Software'), to deal in the Software without\n    restriction, including without limitation the rights to use,\n    copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the\n    Software is furnished to do so, subject to the following\n    conditions:\n\n    The above copyright notice and this permission notice shall be\n    included in all copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\n    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\n    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n    OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkilianc%2Fkoa-jsonp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkilianc%2Fkoa-jsonp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkilianc%2Fkoa-jsonp/lists"}