How to support json and jsonp responses in nodejs + Express?
I have a REST API service written in Node.js + Express that currently supports JSON responses.
Now I need to add support for JSONP responses, so anyone can get it by adding alt = JSONP to the query string.
So my question is, what is the best way to do this?
Perhaps there is a sample or a living example?
Thank!
source to share
The officially supported method looks like this:
First set the name of the JSONP callback parameter:
app.set('jsonp callback name', 'callback');
Then change all instances of res.json
or res.send
to res.jsonp
. For example,res.jsonp(500, { error: 'message' })
If a parameter callback
is specified in the request, the response will be wrapped as JSONP using the function name, but if it isn't, it will just return JSON.
Subsequently this returns JSONP: GET / myapi / v1 / users? Callback = responseCallback
This does not mean: GET / myapi / v1 / users
source to share