Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Servidor Express que devuelve un cuerpo vacío de una solicitud posterior

He estado trabajando en un proyecto con Express y tengo una solicitud de publicación configurada. Sin embargo, devuelve un cuerpo vacío en la respuesta, pero parece estar funcionando de otra manera. Aquí está el servidor:

 const express = require('express'); const app = express(); const http = require('http').Server(app); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(bodyParser.raw()); app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }); app.use("/public", express.static(__dirname + "/public")); http.listen(8080, '0.0.0.0' , function() { console.log('listening on localhost:8080'); }); app.post('/form', (req, res) => { console.log(req.body); res.end('bla bla bla'); // I've also tried res.json and res.send });

Y aquí está el cliente:

 fetch("/form", {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({bla: "bla"})}).then((thing) => { console.log(thing); console.log(thing.json()); console.log(thing.body); });

Parece que debería funcionar, pero en su lugar devuelve este objeto:

 Response body: (...) bodyUsed: true headers: Headers {} ok: true redirected: false status: 200 statusText: "OK" type: "basic" url: "http://localhost:8080/form" [[Prototype]]: Response

Parece que no puedo encontrar dónde se encuentra el error, o incluso si está funcionando y simplemente no sé cómo obtener los datos.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¡Lo he descubierto! Resulta que mi código funcionó todo el tiempo, es solo que había estropeado la solicitud de búsqueda. Lo que debería haber hecho del lado del cliente fue:

 fetch("/form", {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({bla: "bla"})}) .then(res => res.json()) .then(data => console.log(data));

Me faltaba el segundo .then() y eso lo estaba descartando.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!