skip to Main Content

I am very new to node js. I am just testing some stuff about client-server communication.

I have these codes:

server:

app.post('/zsa', (req, res) => {
    res.send("zsamo");
});
client:

fetch("http://localhost:3000/zsa", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(data),
})
  .then((data) => {
    console.log(data);
  });

With this code i get a response object:

enter image description here

I want to log to the console what was in the response data: "zsamo"

2

Answers


  1. fetch returns a Response. If you want JSON from the body of your response then you can do this:

    fetch(url)
      .then(response => response.json())
      .then(data => console.log(data));
    

    See also Using the Fetch API

    Login or Signup to reply.
  2. At client side, in console.log instead of data, write data.body

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search