Middleware in Ash is implemented as a class that has access to the request and the response. A register method is called by the framework and needs to be implemented for the middleware to run.

Since Middleware extends Http, it has access to request properties such as this.params, this.query and so on directly as well as other properties indirectly through this.request and this.response

Middleware should be used in conjunction with services to perform useful operations before the route is executed.

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.

constructor
(
  • context
)

Inherited from Base but overwritten in src/classes/http.js:117

Constructs a new http object. Sets up request and response properties and injects services if defined.

Sets the following properties on the route:

  • request (express request)
  • response (express response)
  • body (request body)
  • params (request url named parameters)
  • query (request url query parameters)
  • headers (request headers)
  • method (request method)

Services are injected under the defined injection property.

Example: Given the following service definition:

static services(register) {
    register('authentication')
  }
  

The route will be able to access the service:

this.authentication
  

Parameters:

  • context Object
    • object with properties request and response which are the express js request and reponse objects respectively
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.

register () public

Registers code to be executed as middleware.

Example:

class Token extends Ash.Middleware {
    services (register) {
      register('store')
      register('current-user')
    }
  
    register () {
      const token = this.query.token
      return this.store.query('user', {filter: {token}}).then(user => {
        this.currentUser.id = user.id
        this.currentUser.name = user.name
      })
    }
  }
  

In the above example we register 2 services, look up a user based on the token in the url query string and then set the returned user as the current user.

Note. Returning a promise from the register function causes later middleware or the route to wait for the promise to resolve as in the example above.

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
  

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