Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Sending JSON in a POST request response via Express

I have a server running Express, and a client written in JS. Using the following code on the client:

document.getElementById("continue").addEventListener("click", function() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    if(username != "" && password != "") {
        const data  = {
            username, password
        };
        console.log(JSON.stringify(data));
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        };
        fetch(urllogin, options).then(response => {
            console.log(response.body);
        });
    }

And the following code on the server:

let port = 9000;

var express = require("express");
var fs = require("fs");
var https = require("https");
var app = express();

app.use(express.json())

app.use((req, res, next) => {
    res.append('Access-Control-Allow-Origin', ['*']);
    res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.append('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

app.post("/login", (req, res) => {
    console.log(req.body);
    response = {
        body:'hello'
    };
    res.json(JSON.stringify(response));
});

https
  .createServer(
    {
      key: fs.readFileSync("server.key"),
      cert: fs.readFileSync("server.cert"),
    },
    app
  )
  .listen(port, function () {
    console.log(`Example app listening on port ${port}! Go to https://localhost:${port}/`);
  });

I successfully receive the JSON object on the server, but on the client, I get:

{"username":"ok","password":"m"} script.js:20:21
ReadableStream { locked: false }
​
locked: false
​
<prototype>: ReadableStreamPrototype { cancel: cancel(), getReader: getReader(), pipeTo: pipeTo(), … }
script.js:29:25

Logged in the console (posted just a part of it).

Why does this happen, and how can I send data as a response to a POST request? Thank you!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda