Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

334
Vistas
app.use(express.raw()) not working in Express.js

I am making an Express application that takes in (binary) post data. Here is my code below.

Server-side:

var express = require('express');
var app = express();
var PORT = 3000;

app.use(express.raw());
    
app.post('/', function(req, res) {
    console.log(req.body);
    res.end();
});

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.listen(PORT, function() {
    console.log("Server listening on port", PORT);
});

index.html:

<script>
  fetch("/", {
    method: "POST",
    body: "hello world",
  });
</script>

However, when I run this code, when logging the request body, it logs an empty object. And when I looked at the documentation, it says that the request body will be an empty object if there was no body to parse. But I had a body in the request. What am I doing wrong here?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

For express.raw() to parse, add "Content-Type": "application/octet-stream" inside headers in your fetch(). Like so:

fetch("/", {
  method: "POST",
  body: "hello world",
  headers: {
    "Content-Type": "application/octet-stream",
  },
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

express.raw() only works if you set your Content-Type header in your fetch to `application/octet stream. So your new fetch method would look like this.

fetch("/", {
    method: "POST",
    body: "hello world",
    headers: {
      "Content-Type": "application/octet-stream"
    }
});

But what if you don't want the content type to be application/octet-stream? In this case, you can specify in the express.raw() options that the content type to parse the body can be any type. How to do this? Replace this:

app.use(express.raw());

With this:

app.use(express.raw({type: '*/*'}));

The type option is the needed content type for body-parser to parse the body, and the */* means that any content type will be accepted.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use this for your usecase app.use(express.text());

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda