【问题标题】:RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:
【发布时间】:2021-10-19 05:26:14
【问题描述】:

这是我打开 http://localhost:5000/express_backend 时的完整错误消息

我正在尝试向服务器发送一个 json 对象,但出现此错误。

下面是我的 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"
  }
  );
}); 

我不知道为什么会这样,我正在尝试向服务器发送一个 json 对象

【问题讨论】:

  • statusCode 参数在 /express_backend 的响应对象中丢失
  • app.use((err,req,res,next)=>{ res.status(404).json({ error : { message : err.message } }); })跨度>
  • 但我已经定义了它^

标签: node.js json express


【解决方案1】:

你打错了,括号[]丢了,应该是

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',
  },
]);

【讨论】:

    猜你喜欢
    • 2023-02-11
    • 2021-08-19
    • 1970-01-01
    • 2018-08-18
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多