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

360
Visualizações
Why the express code returns error if I don't give an empty string?

I have a simple express code which adds two numbers :

const express = require('express')
const bodyParser = require('body-parser')

const app = express()
const port = 3000

app.use(bodyParser.urlencoded({ extended: true }))

app.get('/', (req, res) => {
    res.sendFile(__dirname + "/index.html")
})

app.post("/", (req, res) => {
    let n1 = req.body.num1 //these n1 and n2 are coming from inex.html
    let n2 = req.body.num2
    let result = Number(n1) + Number(n2)
    res.send("" + result)
})

app.listen(port, () => {
    console.log("Server is running at port 3000")
})

In res.send if I removed the empty string and type only res.send(result) the code returns error :

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 5
at new NodeError (node:internal/errors:372:5)
at ServerResponse.writeHead (node:_http_server:275:11)
at ServerResponse._implicitHeader (node:_http_server:266:8)
at ServerResponse.end (node:_http_outgoing:871:10)
at ServerResponse.send (C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\node_modules\express\lib\response.js:232:10)
at C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\calculator.js:17:9
at Layer.handle [as handle_request] (C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\Vandana Singh Bondil\OneDrive\Desktop\Lokesh\calculator\node_modules\express\lib\router\layer.js:95:5)

Can anyone tell me why the code works???

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because send requires body to be...

...a Buffer object, a String, an object, Boolean, or an Array.

Numbers aren't in that list, but strings are. Although it's undocumented in v4 (onwards), in v3 you could pass a response status code (like 200 or 404) as the first argument, and apparently it still supports doing that (despite being undocumented) in the version you're using, probably to support legacy projects. 5 isn't a valid response status code, so you get the error. You have to convert your number to string. "" + result is one way to do that. Others are String(result) or result.toString().

about 4 years ago · Juan Pablo Isaza Relatório
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