rest.ts
rest.ts copied to clipboard
Status codes w/ express?
I don't see how I would send status codes with the buildRouter API -- help?
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.
But if I send it manually, I still have to return the value as well? That's what confused me.
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.