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

197
Views
ExpressJS res.send () no envía los datos

No estoy seguro de por qué la solicitud GET no puede devolverme los datos
Básicamente, mi solicitud GET se ve así:

 router.get('/', (req, res) => { res.set('Content-Type', 'text/html') res.status(200).send(Buffer.from('<p>some html</p>')) });

Luego uso supertest para probar esta ruta.

 test('test', async () => { const res = await request(app) .get('/') expect(res.statusCode).toBe(200); expect(res.body.toString()).toBe("<p>some html</p>"); });

Entonces yo console.log(res.body) , me devuelve el {} vacío
No estoy seguro de por qué el retorno de la ruta me da el obj vacío.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Los datos de respuesta deben obtenerse de res.text no de res.body .

app.js :

 const app = require('express')(); app.get('/', (req, res) => { res.set('Content-Type', 'text/html'); res.status(200).send(Buffer.from('<p>some html</p>')); }); module.exports = app;

app.test.js :

 const app = require('./app'); const request = require('supertest'); describe('71328567', () => { test('should pass', async () => { const res = await request(app).get('/'); expect(res.type).toBe('text/html'); expect(res.text).toBe('<p>some html</p>'); }); });

Resultado de la prueba:

 PASS stackoverflow/71328567/app.test.js 71328567 ✓ should pass (26 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.657 s
about 4 years ago · Juan Pablo Isaza 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!