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:
I want to log to the console what was in the response data: "zsamo"
zsamo
is not JSONres.send("zsamo")
). Beware thatJSON.parse("zsamo")
in a modern browser will still produce SyntaxError: Unexpected token 'z', "zsamo" is not valid JSON ButJSON.parse('"zsamo"')
will result in a string with the content:zsamo
. See this answerzsamo
is not JSON.