You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**IMPORTANT:** You must either use a callback like `res.send()`**OR**`return` a value. Otherwise the execution will hang and no data will be sent to the user. Also, be sure not to return `undefined`, otherwise it will assume no response.
263
263
264
+
### A Note About Flow Control
265
+
266
+
While callbacks like `res.send()` and `res.error()` will trigger a response, they will not necessarily terminate execution of the current route function. Take a look at the following example:
267
+
268
+
```javascript
269
+
api.get('/users', (req,res) => {
270
+
271
+
if (req.headers.test==='test') {
272
+
res.error('Throw an error')
273
+
}
274
+
275
+
return { foo:'bar' }
276
+
})
277
+
```
278
+
279
+
The example above would not have the intended result of displaying an error. `res.error()` would signal Lambda API to execute the error handling, but the function would continue to run. This would cause the function to `return` a response that would override the intended error. In this situation, you could either wrap the return in an `else` clause, or a cleaner approach would be to `return` the call to the `error()` method, like so:
280
+
281
+
```javascript
282
+
api.get('/users', (req,res) => {
283
+
284
+
if (req.headers.test==='test') {
285
+
returnres.error('Throw an error')
286
+
}
287
+
288
+
return { foo:'bar' }
289
+
})
290
+
```
291
+
292
+
`res.error()` does not have a return value (meaning it is `undefined`). However, the `return` tells the function to stop executing, and the call to `res.error()` handles and formats the appropriate response. This will allow Lambda API to properly return the expected results.
293
+
264
294
## Route Prefixing
265
295
266
296
Lambda API makes it easy to create multiple versions of the same api without changing routes by hand. The `register()` method allows you to load routes from an external file and prefix all of those routes using the `prefix` option. For example:
@@ -382,7 +412,7 @@ The `status` method allows you to set the status code that is returned to API Ga
0 commit comments