首页 > 解决方案 > 节点/快递错误,RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:ENOENT

问题描述

从角度应用程序发布数据时(根据发布请求),我收到此错误。其他请求工作正常。

RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:ENOENT

at ServerResponse.writeHead (_http_server.js:248:11)
at ServerResponse._implicitHeader (_http_server.js:239:8)
at write_ (_http_outgoing.js:650:9)
at ServerResponse.end (_http_outgoing.js:760:5)
at ServerResponse.send (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\response.js:221:10)
at ServerResponse.json (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\response.js:267:15)
at D:\My project\graph final\frontend\FindGrapher-backend-final\app.js:42:9
at Layer.handle_error (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\router\index.js:315:13)
at D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\router\index.js:335:12)
at next (D:\My project\graph final\frontend\FindGrapher-backend-final\node_modules\express\lib\router\index.js:275:10)

代码表单后端注册端点

exports.signUp= async (req, res, next) => {

    const newUser = new User({
        name: req.body.name,
        country: req.body.country,
        city:req.body.city,
        email: req.body.email,
        password: req.body.password,
        contactNo: req.body.contactNo,
        gender: req.body.gender,
    });
 try {
        await newUser.save();
    } catch (err) {
        const error = new HttpError(
            'Signing up failed, please try again.',
            500
        );
        res.json({
            message:  'Signing up failed, please try again.',
            error: error
        });
        return next(error);
    }

    const token = jwt.sign({email: req.body.email}, "token_validator", { expiresIn: "1h"});
    res.status(201).json({
        message: "SignUp Succsessfull",
        token: token
    });
};

标签: node.jsexpress

解决方案


你能把你的渔获改为:

 catch (err) {
        res.status(500).json({
            message:  'Signing up failed, please try again.',
            error: error});
    }

您收到该错误是因为您没有设置正确的错误状态。


推荐阅读