简体   繁体   中英

Why does Express.js res.send(jsonObject.property) crash the app?

Using response.send() with a JSON object property crashes the app.

const express = require('express')
const https = require('https')
const app = express()

app.get('/', function(req, res) {

  const url = 'https://api.openweathermap.org/data/2.5/weather?q=London&appid=myID&units=metric'
  
  https.get(url, function(response) { 
    response.on('data', function(data){
      const weatherData = JSON.parse(data)
      const temp = weatherData.main.temp
      const weatherDescription = weatherData.weather[0].description
      res.send(temp)
    })
  })
})

app.listen(3000, () => {
  console.log('We are up and running... d(-_^)')
})

Using nodemon the bash error message is:

express deprecated res.send(status): Use res.sendStatus(status) instead at app.js:16:11
_http_server.js:248
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: -0.78
    at ServerResponse.writeHead (_http_server.js:248:11)
    at ServerResponse._implicitHeader (_http_server.js:239:8)
    at ServerResponse.end (_http_outgoing.js:763:10)
    at ServerResponse.send (C:path\node_modules\express\lib\response.js:221:10)
    at IncomingMessage.<anonymous> (C:path\app.js:16:11)
    at IncomingMessage.emit (events.js:311:20)
    at IncomingMessage.Readable.read (_stream_readable.js:512:10)
    at flow (_stream_readable.js:989:34)
    at resume_ (_stream_readable.js:970:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'ERR_HTTP_INVALID_STATUS_CODE'

The interesting thing is that using a string beforehand or simply using two different JSON properties works:

res.send('' + temp)

or

res.send(weatherDescription + temp)

What is the reason behind Express / Node crashing? Would you mind explaining why this is the case?

Thank you in advance!

See the API reference :

res.send([body])
Sends the HTTP response.

The body parameter can be a Buffer object, a String, an object, Boolean, or an Array. For example:

You are passing in a Number, which isn't one of the valid value types.

(It used to be that passing in a number as the first parameter would set the status code, but that functionality is deprecated now, hence your first line of output).


The interesting thing is that using a string beforehand

someString + someNumber will give you a string, which is one of the valid value types.

Well when you specify number the res.send() express expects a valid status code. In this case you are putting the temp -0.78 which is not a valid status code. But when you specify a '' or a string or after the expression is converted into a string. That is considered as a valid response not a statusCode. Hope that answers your question.

let n = "3"+4
console.log(typeof n)

try to run this snippet. You will see why its converted into a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM