The Ash route class extends the See Http class and so has access to request and response properties.
Routes execute via a series of hooks in the following order
- deserialize
- beforeModel
- model
- afterModel
- serialize
If a hook returns a promise, the subsequent hook will not execute until the promise has resolved.
All hooks are optional except for model
and amything returned from the model
hook will be returned
to the client.
Routes support the following:
- mixins (See Mixin)
- services (See Service)
- middleware (See Middleware)
Methods
-
types
Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.
Example:
this.accepts(['json', 'text'])
// => "json"
Note: This is a proxy of the express js request.accepts
method.
Parameters:
-
types
Mixed- may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array.
Returns:
Returns the best match, or if none of the specified content types is acceptable, returns false.
The first hook to be executed during the lifecycle of a route. This hook should generally be used to perform operations on an incoming request body. As such it makes more sense to use this hook for POSTs, PUTs and PATCHs rather than GETs and DELETEs.
-
type
Determines if the incoming request’s “Content-Type” HTTP header field matches the MIME type specified by the type parameter.
Note: This is a proxy of the express js request.is
method.
Example:
// When Content-Type is application/json
this.is('json')
this.is('application/json')
this.is('application/')
// => true
Parameters:
-
type
String
Returns:
Returns true
if the incoming request’s “Content-Type” HTTP header field matches
the MIME type specified by the type parameter. Returns false
otherwise.
-
register
Route middleware
You can use this to register route specific middleware. ie. middleware specified here
will only run for this route. You can register the same piece of middleware in multiple
routes but you must do so explicitly by registering it in that routes middleware
method.
Call register
with the name of the middleware in the app/middleware
directory that
you want to load. You can call register multiple times to register more than one middleware
and middleware will be executed in the order registered.
Example: registering middleware on a route
// app/routes/my-route.js
import Ash from 'ash-core'
export default class MyRoute extends Ash.Route {
static middleware (register) {
register('my-middleware')
}
}
Parameters:
-
register
Function
headers
ObjectThe request headers object. Contains Key-value pairs of header names and values. Header names are lower-cased.
Duplicates in raw headers are handled in the following ways, depending on the header name:
Duplicates of age, authorization, content-length, content-type, etag, expires, from, host, if-modified-since, if-unmodified-since, last-modified, location, max-forwards, proxy-authorization, referer, retry-after, or user-agent are discarded. set-cookie is always an array. Duplicates are added to the array. For all other headers, the values are joined together with ', '.
Example:
this.headers
// { 'user-agent': 'curl/7.22.0',
// host: '127.0.0.1:3010',
// accept: '*\/*' }
method
ObjectContains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.
Example:
this.method // GET
params
ObjectNamed url parameters
This property is an object containing properties mapped to named route parameters. This object defaults to {}.
Example: defining parameters
// app/router.js
Router.map(function () {
this.route('users', {path: '/users/:user_id'})
})
Example: accessing defined parameters
this.params.user_id
query
ObjectThis property is an object containing a property for each query string parameter in the route. If there is no query string, it is an empty object
Example:
// /users?age=20&name=bob
this.query.age // 20
this.query.name // bob
request
ObjectThe http request object.
This is the express request object See http://expressjs.com/en/api.html#req
response
ObjectThe http response object.
This is the express response object See http://expressjs.com/en/api.html#res
routeName
String publicThe name of the route. This is the same as the name of the route js file (without the .js)
and not the name of the exported class. For the name of the class use this.name