RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:

Issue

This is the full error message when I open http://localhost:5000/express_backend

enter image description here

I am trying to send a json object to the server and I got this error.

Below is my server.js

const express = require('express'); //Line 1
const app = express(); //Line 2
const port = process.env.PORT || 5000; //Line 3

app.listen(port, () => console.log(`Listening on port ${port}`)); 

app.use((err,req,res,next)=>{
   res.status(404).json({
       error : {
           message : err.message
      }
   });
})

// create a GET route
app.get('/express_backend', (req, res) => { //Line 9
  res.send(
    {
      name: "Tester one",
      techs: ["React & Redux", "Express", "MySQL"],
      description: "Lorem ipsum doabore et",
      checkin: "09:00",
      checkout:"18:30",
      type:"Known"
  },
  {
      name: "Tester two",
      techs: ["React & Redux", "Express", "MySQL"],
      description: "Lorem ipsum dolor sit amet, consectetet",
      checkin: "09:00",
      checkout:"18:30",
      type:"Blacklisted"
  }
  );
}); 

I am not sure why this is happening, I am trying to send an json objects to the server

Solution

You have a typo, you lost the brackets [], it should be

res.send([
  {
    name: 'Tester one',
    techs: ['React & Redux', 'Express', 'MySQL'],
    description: 'Lorem ipsum doabore et',
    checkin: '09:00',
    checkout: '18:30',
    type: 'Known',
  },
  {
    name: 'Tester two',
    techs: ['React & Redux', 'Express', 'MySQL'],
    description: 'Lorem ipsum dolor sit amet, consectetet',
    checkin: '09:00',
    checkout: '18:30',
    type: 'Blacklisted',
  },
]);

Answered By – slideshowp2

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published