frontexpress/docs/api.md
2017-06-22 21:17:18 +02:00

6.4 KiB

API

Method Short description
Frontexpress
frontexpress() Creates an instance of application
frontexpress.Router() Creates a Router object
frontexpress.Middleware Returns the Middleware class
Application
set(setting, value) Assigns a setting
listen(callback) Starts the application
route(uri) Gets a Router initialized with a root path
use(uri, middleware) Sets a middleware
get(uri, middleware) Applies a middleware on given path for a GET request
post(uri, middleware) Applies a middleware on given path for a POST request
put(uri, middleware) Applies a middleware on given path for a PUT request
delete(uri, middleware) Applies a middleware on given path for a DELETE request
httpGet(request, success, failure) Invokes a GET ajax request
httpPost(request, success, failure) Invokes a POST ajax request
httpPut(request, success, failure) Invokes a PUT ajax request
httpDelete(request, success, failure) Invokes a DELETE ajax request
Router
use(middleware) Sets a middleware
all(middleware) Sets a middleware on all HTTP method requests
get(uri, middleware) Applies a middleware on given path for a GET request
post(uri, middleware) Applies a middleware on given path for a POST request
put(uri, middleware) Applies a middleware on given path for a PUT request
delete(uri, middleware) Applies a middleware on given path for a DELETE request
Middleware
entered(request) Invoked by the app before an ajax request is sent
exited(request) Invoked by the app before a new ajax request is sent
updated(request, response) Invoked by the app after an ajax request has responded
failed(request, response) Invoked by the app after an ajax request has failed
next() Allows to break the middleware chain execution

middleware function

After registering a middleware function, the application invokes it with these parameters:

  (request, response, next) => {
    next();
  }

request: Object, the ajax request information sent by the app

response: Object, the response of request

next: Function, the next() function to call to not break the middleware execution chain

request object

  {
    method,
    uri,
    headers,
    data,
    history: {
      state,
      title,
      uri
    }
  }

method: String, HTTP methods 'GET', 'POST'...

uri: String, path

headers: Object, custom HTTP headers

data: Object, data attached to the request

history: Object, object with properties state, title and uri

If the history object is set, it will activate the browser history management. See browser pushState() method for more information about state, title, and uri (url). uri and history.uri can be different.

params: Object, object containing the path parameters

response object

  {
    status,
    statusText,
    responseText,
    errorThrown,
    errors
  }

status: Number, HTTP status 200, 404, 401, 500...

statusText: String

responseText: String response content

errorThrown: Object exception thrown (if request fails)

errors: String error description (if request fails)