@eggjs/jsonp

An egg plugin for jsonp support.
Usage
egg built-in plugin jsonp is enabled by default.
export default {
jsonp: {
enable: true,
},
};
Configuration
- {String|Array} callback - jsonp callback method key, default to
[ '_callback', 'callback' ]
- {Number} limit - callback method name's max length, default to
50
- {Boolean} csrf - enable csrf check or not. default to false
- {String|RegExp|Array} whiteList - referrer white list
if whiteList's type is RegExp, referrer must match whiteList, pay attention to the first ^ and last /.
export default {
jsonp: {
whiteList: /^https?:\/\/test.com\//,
},
};
if whiteList's type is String and starts with .:
export default {
jsonp: {
whiteList: '.test.com',
},
};
if whiteList's type is String and not starts with .:
export default {
jsonp: {
whiteList: 'sub.test.com',
},
};
whiteList also can be an array:
export default {
jsonp: {
whiteList: ['.foo.com', '.bar.com'],
},
};
see config/config.default.ts for more detail.
API
- ctx.acceptJSONP - detect if response should be jsonp, readonly
Example
Standard Application Usage
In app/router.ts
const jsonp = app.jsonp();
app.get('/default', jsonp, 'jsonp.index');
app.get('/another', jsonp, 'jsonp.another');
app.get('/customize', app.jsonp({ callback: 'fn' }), 'jsonp.customize');
tegg HttpController Usage
TODO: TBD
Questions & Suggestions
Please open an issue here.
License
MIT
Contributors

Made with contributors-img.