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

Status codes w/ express? #23

Open
asktree opened this issue Jun 20, 2020 · 3 comments
Open

Status codes w/ express? #23

asktree opened this issue Jun 20, 2020 · 3 comments
Labels
question Further information is requested solved

Comments

@asktree
Copy link

asktree commented Jun 20, 2020

I don't see how I would send status codes with the buildRouter API -- help?

@hmil
Copy link
Owner

hmil commented Jun 21, 2020

The route handlers retain their original express signature (req, res).

You can use the res object to send a custom status code. For instance: res.status(201).

Note that if you chose to use the res object, then you must send your response manually using the regular express api (res.send() or res.write(), etc...). rest.ts ignores the returned value as you can see here.

Here is an example of how to do this:

buildRouter(myCustomAPI, (builder) => builder
     .listPublications(async (req, res) => {
         res.status(404).send('Sorry, we cannot find that!');
     });

_Note that marking the function async is a good idea to make sure that res.send is invoked before the promise returned by the handler resolves.

@hmil hmil added question Further information is requested solved labels Jun 21, 2020
@asktree
Copy link
Author

asktree commented Jun 22, 2020

But if I send it manually, I still have to return the value as well? That's what confused me.

@hmil
Copy link
Owner

hmil commented Jun 23, 2020

No if you use the res object then you need to send the response body using the res object. The return value will be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested solved
Projects
None yet
Development

No branches or pull requests

2 participants