Hey Express team!
First of all, great work! Congrats to all of the maintainers of this OSS. While I tried to use some ES6 features such as Map and Set, I realized that the following code doesn't work as expected and tho didn't know the exact implementation of res.json I wanted to ask to all.
My question is: why does the following code returns {} to the client-side?
router.get('/test', (req, res, next) => {
const response = new Map();
response.set('hello', 'world');
res.json(response);
});
Thanks!
Hi! The reason is because that is just how JavaScript converts a Map to JSON:
$ node -e 'm=new Map();m.set("hello","world");console.log(JSON.stringify(m))'
{}
and tho didn't know the exact implementation of res.json I wanted to ask to all.
You should be able to find all kinds of information, including this, on our website https://expressjs.com/ 馃憤
http://expressjs.com/en/4x/api.html#res.json
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().