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.redirect unusable in async route handler. #1916

Open
3 tasks done
robinp7720 opened this issue Jun 7, 2022 · 2 comments
Open
3 tasks done

res.redirect unusable in async route handler. #1916

robinp7720 opened this issue Jun 7, 2022 · 2 comments

Comments

@robinp7720
Copy link

robinp7720 commented Jun 7, 2022

  • Used appropriate template for the issue type
  • Searched both open and closed issues for duplicates of this issue
  • Title adequately and concisely reflects the feature or the bug

Restify Version: 9.0.0-rc.3
Node.js Version: v16.13.0

Expected behaviour

Redirect shouldn't need the next callback parameter anymore.

Actual behaviour

Impossible to use res.redirect from an async route handler

Repro case

Call res.redirect from an async route handler

Cause

The direct function requires the next callback function to be passed.

Are you willing and able to fix this?

Yes, but I'm not sure how it should be fixed, or if this is even intentional.

@josephharrington
Copy link
Contributor

josephharrington commented Jun 17, 2022

Thanks for the heads up! Until we can get a fix in, you might be able to work around this in the meantime by doing something like:

async function helloworldRedirect(req, res) {
  return res.redirect('./helloworld', (v) => v);
}

Passing a next function like (v) => v and returning the return value of res.redirect should effectively accomplish the same thing as passing next in a non-async handler if I'm reading the code right.

@carycodes
Copy link

Another alternative: use a callback handler that wraps an async function with your actual functionality:

function redirectHandler(req, res, next) {
  redirectHandlerImpl(req)
    .then(url => res.redirect(url, next))
    .catch(next)
}

async function redirectHandlerImpl(req) {
  // do some stuff...
  return url
}

This still feels like a bit of a hack though. Restify needs an official way to invoke redirects from async handlers.

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

3 participants