Express: Question about ES6 Map with Express

Created on 23 Sep 2018  路  2Comments  路  Source: expressjs/express

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!

question

All 2 comments

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().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Domiii picture Domiii  路  3Comments

prashantLio picture prashantLio  路  3Comments

Sunriselegacy picture Sunriselegacy  路  3Comments

ZeddYu picture ZeddYu  路  3Comments

jefflage picture jefflage  路  4Comments