Azoulay Jason
Azoulay Jason

Reputation: 2989

statusMessage is undefined in "res" - Node Express

I'm working with Node.js & Express.js and I need a route to show the status of the app.

router.get('/status', function(req, res) {
   res.send("status " + res.statusCode + " : " + res.statusMessage);
});

When I log res, both statusCode and statusMessage are null.

The result is status 200 : undefined

Am I doing something wrong ?

Upvotes: 3

Views: 2365

Answers (2)

Anand Undavia
Anand Undavia

Reputation: 3543

You are doing nothing wrong. Take a look the source code of ServerResponse here.

As you can see, statusCode and statusMessage are initialized (line) as:

ServerResponse.prototype.statusCode = 200;
ServerResponse.prototype.statusMessage = undefined;

And because you are not modifying either of them, they are set to defaults.

Upvotes: 1

Quentin
Quentin

Reputation: 943510

When I log res, both statusCode and statusMessage are null.

Look at your output:

status 200 : undefined

statusCode is not null, it is 200.

statusMessage is not null either, it is undefined. This is expected. Look at the documentation for it:

If this is left as undefined then the standard message for the status code will be used.

The default message won't be added until after the object has left your control. As far as I know, there is no way to find out what message it will get other than inferring it from the status code.

Upvotes: 2

Related Questions