Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res.status() and mung.json() #6

Closed
hk0i opened this issue Oct 29, 2016 · 8 comments
Closed

res.status() and mung.json() #6

hk0i opened this issue Oct 29, 2016 · 8 comments

Comments

@hk0i
Copy link

hk0i commented Oct 29, 2016

It seems like res.status() and mung.json() don't play well together.
I'm fairly new to express in general so I may be doing something wrong here, but I would have expected to be able to do something like:

//untested pseudo-code, I can cook up a better example that
//fully illustrates the problem if necessary.
function getSomething(req, res) {
  //hack because there seems to be no way to get status from `res` in express:
  res.locals.statusCode = 404;
  //set status code to 404 and send json
  res.status(404).json({message: 'Resource not found'});
}

function addMeta(body, req, res) {
  //this function *never* gets called when calling `res.status()` as in the `getSomething()` function.
  body.statusCode = res.locals.statusCode;
}

app.use(mung.json(addMeta));
app.get('/something', getSomething);

Once res.status() is called, the addMeta method never gets called it seems.

What I'm really trying to do here is provide a way for my api to slap on extra meta data at the end of the json response with the http status code for clients that may not be able to receive non-200 http status codes. It would be great to be able to, from the addMeta function in my example, make a call like res.getStatus() and check the status code that has been set by res.status() in the previous middleware or route or whatever.

@richardschneider
Copy link
Owner

richardschneider commented Oct 29, 2016

It's been designed to work with res.status, see the test.

I think your problem is that getSomething never calls res.end. The last line should be:

res.status(404).json({message: 'Resource not found'}).end();

See this discussion about res.end() on stackoverflow.

@hk0i
Copy link
Author

hk0i commented Oct 29, 2016

Awesome! That worked. Is there a way to retrieve the status code on the receiving end?

I'm also now having issues now with async methods not getting "munged." I could open a separate ticket for that though if it's better, but something like this:

        const connection = mysql.createConnection(mysqlConf);
        connection.connect();

        connection.query('SELECT 1 + 1 as result', function(err, rows, fields) {
            if (err) throw err;
            res.json(rows).end();
        });

is avoiding the munge as well.

@richardschneider
Copy link
Owner

On the server side, you can use res.statusCode.

This should work:

function addMeta(body, req, res) {
    body.statusCode = res.statusCode;
}

On the receiver side, it depends on the package you are using to call the server.

@hk0i
Copy link
Author

hk0i commented Oct 30, 2016

res.statusCode only seems to be working for 200 status codes for me.
When I try

        res.status(404).json(response).end();

It doesn't work.

Edit: I do get the http response code 404, but the munge function doesn't seem to trigger, is there something I have to do to make it work for non-200 responses?

@richardschneider
Copy link
Owner

Let me write a test. Talk to you in 20 mins.

@richardschneider
Copy link
Owner

Published v0.4.6 that allows munging of error responses.

Try this:

app.use(mung.json(addMeta, { mungError: true } ));

@hk0i
Copy link
Author

hk0i commented Oct 30, 2016

Works like a charm. Thanks!

It's also safe to ignore that async mysql thing I was talking about before. I'm not sure what the issue was but I seem to have solved that as well.

This error munging is great because now I can check for some parameter and pass everything as http 200 if needed and append the actual status code in the JSON response. I really appreciate the effort 👍

@richardschneider
Copy link
Owner

No worries mate, I appreciate the feed back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants