ishan avishka
ishan avishka

Reputation: 75

node/express error, RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: ENOENT

I'm getting this error when posting data from a angular app(on post request). Other requests are working properly.

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: 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)

code form backend signUp endpoint

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
    });
};

Upvotes: 0

Views: 3014

Answers (2)

Mara
Mara

Reputation: 403

I got this error because I've tried to add an image in folder that don't exist. So back-end send me this error, and front-end send me unexpected token < at position 0 because of back-end incorrect response.

Upvotes: 1

Danizavtz
Danizavtz

Reputation: 3270

Can you change your catch to:

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

You are receiving that error because you are not putting an proper error status.

Upvotes: 0

Related Questions