Serve JSONP in Dyson

Dyson is a great fake JSON server written in JavaScript/Node.JS.

In my current pet project, I have a fake Strava API server. Strava REST API allows you to use JSONP to overcome cross-domain restrictions. You have to add a callback parameter to the query string.

To simulate it with Dyson, you have to override the render method of the Express middleware in the endpoint definition. In my solution, depending on the existence of the callback parameter, I return either raw JSON response or I wrap it with the provided callback:

function renderJsonp(res, callback) {
res.append('Content-Type', 'application/javascript');
res.send(callback + '(' + JSON.stringify(res.body) + ');');
function renderJson(res) {
function render(req, res) {
var callback = req.query.callback;
if (callback) renderJsonp(res, callback);
else renderJson(res);
module.exports = {
path: '/api/v3/athlete/activities',
template: {
// …
render: render