Home > Software design >  RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:

Time:10-19

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

CodePudding user response:

Typo error, 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',
  },
]);
  • Related