DEV Community 👩‍💻👨‍💻

Dhanush N
Dhanush N

Posted on

NodeJs Response

Consider a Nodejs express app

I am setting a new parameter 'abc' with a string to the response object

If I send a response to frontend will they be able to view the value of abc

Nodejs

If you inspect via browser, you will be able to see the

  • Headers
  • Cookies
  • Response Body etc

But is there any possibility for the value 'abc' to be got by the frontend ?

Top comments (7)

Collapse
adam_cyclones profile image
Adam Crockett

A custom key on the response object, I don't think that's how it's done.

Browser makes request, server sends response, response could headers and cookies, custom x- headers, custom cookies, or even something more exotic like server sent events or websockets, you could even inject a script into the body of the page with the values in the global scope.

Collapse
dasrajanraj profile image
Rajan Raj Das

Dhanush, you can add it in the header of the response or in the body of the response(classical way)

  1. To add in the header
    res.setHeader("abc", "Finding out")

  2. To add in the body
    res.status(200).json({result : "Message Successful", abc : "Finding Out"})

Collapse
dhanushnehru profile image
Dhanush N Author

Hey Rajan,
Thanks for your input

I know that we can add it by setting header, but my question is if we don't add it in header & only assign the value can it be got from frontend.

Collapse
dasrajanraj profile image
Rajan Raj Das • Edited on

No, you cannot do that. If you see the basic of Http response packet. It has header section( with several sub heading ) and a response body. These are the protocol that guide the communication between client and server.
If you are directly adding a field ( i.e. abc ) in the response , it cannot create its own section apart from what is defined in the protocol ,right?
I think this would answer you question.

Collapse
adam_cyclones profile image
Adam Crockett

Your want to send data to the frontend correct?

Collapse
dhanushnehru profile image
Dhanush N Author

I don't want to send it to the frontend

I am asking like if a variable is set, is there any possibility to get the value of abc from frontend, by any means

Collapse
adam_cyclones profile image
Adam Crockett

Gotcha see above comment

🤔 Did you know?

 
🌚 Dark mode is available in Settings.