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

  1. deserialize
  2. beforeModel
  3. model
  4. afterModel
  5. 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)
Show:
accepts
(
  • types
)
Mixed

Inherited from Http: src/classes/http.js:198

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:

Mixed:

Returns the best match, or if none of the specified content types is acceptable, returns false.

afterModel
(
  • model
)
Any
public

Parameters:

Returns:

Any
beforeModel () public
constructor () public

Inherited from Base but overwritten in src/classes/route.js:34

deserialize () public

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.

error
(
  • error
)
Any
public

Parameters:

Returns:

Any:

error

hasMiddleware () private
is
(
  • type
)
Mixed

Inherited from Http: src/classes/http.js:219

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:

Returns:

Mixed:

Returns true if the incoming request’s “Content-Type” HTTP header field matches the MIME type specified by the type parameter. Returns false otherwise.

Route.middleware
(
  • register
)
static public

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:

model () public
registeredMiddleware () private
serialize
(
  • model
)
public

Parameters:

body

Object

Inherited from Http: src/classes/http.js:40

The request body

Contains key-value pairs of data submitted in the request body

headers

Object

Inherited from Http: src/classes/http.js:84

The 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

Object

Inherited from Http: src/classes/http.js:105

Contains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.

Example:

this.method // GET
  

modelName

Unknown public

params

Object

Inherited from Http: src/classes/http.js:47

Named 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

Object

Inherited from Http: src/classes/http.js:69

This 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

Object

Inherited from Http: src/classes/http.js:22

The http request object.

This is the express request object See http://expressjs.com/en/api.html#req

response

Object

Inherited from Http: src/classes/http.js:31

The http response object.

This is the express response object See http://expressjs.com/en/api.html#res

routeName

String public

The 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