0

why the parameter data different in console and website

i need use JSON.stringify or JSON.parse in res.end?

  • In order to debug something in the console, you would use `JSON.stringify` (otherwise you would just see [object object] or similar). IN `res.send` you can send a lot of data-formats (depending on the content-type set in the headers etc.), also JSON and strings.. see [here](https://stackoverflow.com/questions/29555290/what-is-the-difference-between-res-end-and-res-send) for more info on res.end & res.send.. – iLuvLogix Dec 20 '21 at 13:16

2 Answers2

0

I think this code will help you.

res.end(data.toString())

I'll explain the reason why it is different in console and website. In console, the data is type of buffer, so it can probably output data in byte format. But in the web browser, the data is parsed in JSON string, so it'll be converted to string and correct result comes out.

Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
JUPITER
  • 1
  • 2
0

The JSON.stringify() method converts a JavaScript object or value to a JSON string, so add this to your res.end()

res.end(JSON.stringify(data));

Ran Turner
  • 14,906
  • 5
  • 47
  • 53