src/index.js

Build Status

The Nxus router is an Express compatible web server and router for Nxus applications.

Installation

In your Nxus application:

> npm install @nxus/router --save

Usage

Defining a route

app.get('router').route('/', (req, res) => {
  res.send('Hello World')
})

Alternatively, you can specify a HTTP method:

app.get('router').route('GET', '/', (req, res) => {
  res.send('Hello World')
})

Adding Express/connect middleware

app.get('router').middleware((req, res) => {
  res.status(404).send('Nothing to see here');
})

Adding static files/directories

app.get('router').static("my-prefix", myPath)

For example, myFile.txt in myPath is then available at the url /my-prefix/myFile.txt

Sometimes its good to have a static assets folder where all your assets live. For that reason, you can use the assets gatherer.

API


Router

src/index.js

Instance members

#constructor(app)

Sets up the relevant gather/providers

constructor(app)

src/index.js

Sets up the relevant gather/providers

Parameters

  • app :
#getExpressApp

Returns the Express App instance.

getExpressApp

src/index.js

Returns the Express App instance.

Returns

Instance :

ExpressJs app instance.

#getRoutes

Returns the internal routing table.

getRoutes

src/index.js

Returns the internal routing table.

Returns

array :

routes which have been registered

#launch

Launches the Express app. Called by the app.load event.

launch

src/index.js

Launches the Express app. Called by the app.load event.

#setMiddleware(route, handler, method)

Adds a middleware handler to the internal routing table passed to Express. Accessed with 'middleware' gather.

setMiddleware(route, handler, method)

src/index.js

Adds a middleware handler to the internal routing table passed to Express. Accessed with 'middleware' gather.

Parameters

  • string route :

    A URL route.

  • function handler :

    An ExpressJs type callback to handle the route.

  • method (default 'use') :
#setRoute(method, route, handler)

Adds a route to the internal routing table passed to Express. Accessed with the 'route' gather.

setRoute(method, route, handler)

src/index.js

Adds a route to the internal routing table passed to Express. Accessed with the 'route' gather.

Parameters

  • string method :

    Either 'get', 'post', 'put' or 'delete'.

  • string route :

    A URL route.

  • function handler :

    An ExpressJs type callback to handle the route.

#setStatic(prefix, path)

Adds a path to serve static files.

setStatic(prefix, path)

src/index.js

Adds a path to serve static files.

Parameters

  • string prefix :

    The path at which the static files will be accessible. For example: /js

  • string path :

    A fully resolved path.

#stop

Stops the express app. Called by the app.stop event.

stop

src/index.js

Stops the express app. Called by the app.stop event.