Route.get('/', ({ response }) => {
response.send('hello world')
})
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
This guide gives an outline on how to make use of the HTTP Response object to make the response.
The Node.js raw res object can be accessed as response.response .
|
AdonisJs passes the current HTTP response object as part of the context which is sent to all route handlers and middleware.
Route.get('/', ({ response }) => {
response.send('hello world')
})
Let’s see how to send an array of users back as JSON from the route.
Route.get('/', async ({ response }) => {
const users = await User.all()
response.send(users)
})
Also, you can use response.json
method as an alias to response.send
As of 4.0, you can also return
values from the route closure or controller method, instead of using dedicated response
methods.
The following is equivalent to response.send
or response.json
but feels more natural with a return statement.
Route.get('/', async () => {
return await User.all()
})
Since the request/response lifecycle allows you to return values and calling dedicated methods, it discourages the usage of callbacks entirely.
Which means the following will not work.
Route.get('/', async ({ response }) => {
fs.readFile('somefile', (error, contents) => {
response.send(contents)
})
})
The reason why above code is not going to work, is that as soon the route handler is executed, AdonisJs ends the response, and your callback gets executed later, resulting in an error.
It may sound scary, but it is done in favor of having a clean codebase, instead of sprinkling mixed API everywhere.
What you can do instead is promisify the callback and use it with await
.
const Helpers = use('Helpers')
const fs = use('fs')
const readFile = Helpers.promisify(fs.readFile)
Route.get('/', async ({ response }) => {
return await readFile('somefile')
})
See how cleaner the code is now. Javascript has a rich eco-system, and it is 100% possible to write code without callbacks just by promisfying them, and as a community, we want to encourage it.
If you think using callbacks is great, we have a way for you to use them. Simply instruct the response
object not to end implicitly
.
Route.get('/', async ({ response }) => {
response.implicitEnd = false
fs.readFile('somefile', (error, contents) => {
response.send(contents)
})
})
Following is the list of methods to set/remove headers.
Only set the header if it does not exist.
response.safeHeader('Content-type', 'application/json')
Also, you can set/remove cookies by calling one of the following methods
response.cookie('cartTotal', 20)
Remove existing cookie by setting the expiry in the past.
response.clearCookie('cartTotal')
Since all cookies are encrypted and signed, it is not possible to read them from frontend Javascript code. In that case, may want to set a plain cookie instead.
// not signed or encrypted
response.plainCookie('cartTotal', 20)
Use one of the following methods to redirect requests to a different URL.
Redirect request to a different url, by default it will set the status as 302
.
response.redirect('/url')
// or
response.redirect('/url', false, 301)
Also, you can send current request params to the other request as well by setting true
as the 2nd param.
response.redirect('/url', true)
Redirect to a route. This method builds the URL automatically from the route name or controller method.
Route
.get('users/:id', 'UserController.show')
.as('profile')
response.route('profile', { id: 1 })
Also, you can pass the controller method.
response.route('UserController.show', { id: 1 })
Since AdonisJs allows registering routes for multiple domains, you can instruct this method to build the URL for a specific domain.
response.route('posts', { id: 1 }, 'blog.adonisjs.com')
The response object makes it super easy to stream files from your server to the client.
Stream the file to the client. This method does not force the client to download the file as an attachment. For example, Browsers may choose to display the file in a new window.
response.download(Helpers.tmpPath('uploads/avatar.jpg'))
Force download the file
response.attachment(
Helpers.tmpPath('uploads/avatar.jpg')
)
With a custom name
response.attachment(
Helpers.tmpPath('uploads/avatar.jpg'),
'myAvatar.jpg'
)
It is also possible to extend the Response
prototype by adding your own methods, known as macros.
If your macros are specific to your application only, you can add the macro inside start/hooks.js
file after the providers have been booted.
const { hooks } = require('@adonisjs/ignitor')
hooks.after.providersBooted(() => {
const Response = use('Adonis/Src/Response')
Response.macro('sendStatus', function (status) {
this.status(status).send(status)
})
})
If you are writing a module/addon for AdonisJs, you can add a macro inside the boot method of your service provider.
const { ServiceProvider } = require('@adonisjs/fold')
class MyServiceProvider extends ServiceProvider {
boot () {
const Response = use('Adonis/Src/Response')
Response.macro('sendStatus', function (status) {
this.status(status).send(status)
})
}
}