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

Possible to return a response in Express 5? #3884

Closed
mikemaccana opened this issue Feb 15, 2019 · 4 comments
Closed

Possible to return a response in Express 5? #3884

mikemaccana opened this issue Feb 15, 2019 · 4 comments

Comments

@mikemaccana
Copy link

I've read #2259 there a single example of a route using Promises? I've read the router changelogs etc, from alpha 2 to alpha 7, but I can't find anything.

I know, eg, https://arc.codes lets me just return a response. It also has middleware that uses this pattern.

exports.handler = async function http(request) {
  return {
    status: 201,
    type: 'text/html; charset=utf8',
    body: `
      <!doctype html>
      <html>
        <body>hello world</body>
      </html>
   `
  }
}

Does express 5 allow me to return a response from a route?

@mikemaccana mikemaccana mentioned this issue Feb 15, 2019
@dougwilson
Copy link
Contributor

dougwilson commented Feb 15, 2019

Hi! We are still working on docs and such, so don't have anything to point to yet.

Does express 5 allow me to return a response from a route?

At least, not like in your example. The API for sending responses is the same as Express 4.

Middleware and handlers can now return promises and if the promise is rejected, next(err) will be called with err being the value of the rejection. The resolved value of the promise is ignored.

@mikemaccana
Copy link
Author

Thanks Doug! I took Promises as 'removing callbacks' and instead it's allowing Promises in more places. I appreciate the response (even if it wasn't the answer I wanted) - have a good one.

@ryhinchey
Copy link

I think this issue can be closed :)

@jonchurch
Copy link
Member

To clarify, the answer is no you cant send a response by returning an object from a route handler. This is a pattern that’s used in Lambda functions, for example.

You can write a helper function and wrap your route handler in it that would send a response based on the object returned, but Express’s method for sending responses is not changing in v5.

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

4 participants