kartikk221 / hyper-express

High performance Node.js webserver with a simple-to-use API powered by uWebsockets.js under the hood.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Response send vs json

7elven opened this issue · comments

I try to transfom express to hyper-express
but response make me confused

.send expect only SendableData = string | Buffer | ArrayBuffer;
.json all of .send but include object

Why .send not include object and Why I can response string on .json ?

Sometimes I want to forwards headers but haders response that I want to forward It's include content-length and it's make app crash

my fixed is delete content-length before response headers

do you have any recommend ?

I try to transfom express to hyper-express but response make me confused

.send expect only SendableData = string | Buffer | ArrayBuffer; .json all of .send but include object

Why .send not include object and Why I can response string on .json ?

The Response.send() method is a pure serializable data sending method meaning you can send a String or a pure data form such as Buffer, ArrayBuffer etc etc.

On the other hand, the Response.json() method does 2 additiona things:

  • Sets the content-type to application/json
  • Applies JSON.stringify(body) on the provided body meaning It is tailored for sending JSON data properly.

With this mind, It should be clear that doing something like Response.json('Hello') would work, It is not proper and you are essentially telling the browser that you are sending JSON but are actually sending a string which gets wrapped in JSON.stringify.

Sometimes I want to forwards headers but haders response that I want to forward It's include content-length and it's make app crash

my fixed is delete content-length before response headers

do you have any recommend ?

Could you provide me a snippet of your code which causes the crash and what the crash you are seeing is?

I am assuming your use case is some sort of proxying but even then hyper-express supports a custom content-length header. You may be encountering a bug which may be worth looking into.

Sometimes I want to forwards headers but haders response that I want to forward It's include content-length and it's make app crash
my fixed is delete content-length before response headers
do you have any recommend ?

Could you provide me a snippet of your code which causes the crash and what the crash you are seeing is?

I am assuming your use case is some sort of proxying but even then hyper-express supports a custom content-length header. You may be encountering a bug which may be worth looking into.

    const url = req.params[0]
    const body = await req.json()
    const { data, headers } = await axios({ ...req, data: body, url })
    res.setHeaders(headers)
    res.json(data)

when headers from axios response contain content-length it will crash
My fixed is

delete headers['content-length']
res.setHeaders(headers)

What does the crash look like in your console? Could you post the error / stack trace you see or does the application just shut down? Could you also log and provide the headers returned by the axios request along with the length of the data.

If the data property is a string then could you additionally also provide the following: Buffer.from(data).byteLength Thanks

This is headers response from axios

Object [AxiosHeaders] {
  date: 'Fri, 17 Nov 2023 13:32:51 GMT',
  'content-type': 'application/json',
  'content-length': '87',
  connection: 'keep-alive',
  vary: 'Origin',
  'access-control-allow-origin': '*',
  'access-control-allow-credentials': 'true',
  'burst-ratelimit-limit': '100',
  'burst-ratelimit-remaining': '100',
  'burst-ratelimit-reset': '1700227971',
  'strict-transport-security': 'max-age=15724800; includeSubDomains'
}

This is data response just simple json

{
  error: 'message msg_7YoJxFpwkEy6sUx4vvpQjF279jG54by5wEVcC8myr9Wc7STZpprqx not found'
}

It's not show error stack it's only return 502

This page isn’t working
HTTP ERROR 502

Could you try upgrading to v6.14.2 to see If your bug is fixed.

it's fixed, test on v6.14.3