svrrouter/README.md

5.7 KiB

SVRouter

SVRouter is a router library built for use in building web applications as SVR.JS 4.x mods.

Example SVR.JS mod that uses SVRouter (you will need to install the svrouter npm package either in the SVR.JS installation root or globally):

const svrouter = require("svrouter");
const router = svrouter();

// eslint-disable-next-line no-unused-vars
router.get("/hello", (req, res, logFacilities, config, next) => {
  res.writeHead(200, "OK", {
    "Content-Type": "text/plain"
  });
  res.write("Hello World!");
  res.end();
});

module.exports = router;

module.exports.modInfo = {
  name: "Example mod with SVRouter",
  version: "0.0.0"
};

Methods (exported by the svrouter package)

svrouter()

Returns: the SVRouter router, which is a SVR.JS mod function with additional functions for adding routes and middleware.

Methods (provided by the SVRouter router)

router(req, res, logFacilities, config, next)

Parameters:

  • req - the SVR.JS request object
  • res - the SVR.JS response object
  • logFacilities - the SVR.JS log facilities object
  • config - the SVR.JS configuration object
  • next - the callback which passes the execution to SVR.JS mods and SVR.JS internal handlers.

The function is a SVR.JS mod callback. You can read more about the SVR.JS mod callbacks in the SVR.JS mod API documentation.

router.route(method, path, callback)

Parameters:

  • method - the HTTP method, for which the route applies (String)
  • path - the route path (begins with "/"), for which the route applies. The route paths are process via the path-to-regexp library (String)
  • callback - the SVR.JS mod callback applied for the route (Function)

Returns: the SVRouter router (so that you can chain the methods for adding routes or pass-throughs)

The function adds a route to the SVRouter router.

If the method parameter is "*", the route will apply to all the methods

The callback parameter has these arguments of the SVR.JS mod callback:

  • req - the SVR.JS request object
  • res - the SVR.JS response object
  • logFacilities - the SVR.JS log facilities object
  • config - the SVR.JS configuration object
  • next - the callback which passes the execution to other routes, SVR.JS mods and SVR.JS internal handlers.

The req object has an additional params property, which contains request parameters, for example if the request URL is /api/task/1, and the route path is /api/task/:id, then the req.params object is a null prototype object with the id property set to "1".

The req object has another additional svrouterBase property, which contains the route base used internally by a SVRouter router. It's not recommended to override this property, as doing it may result in a SVRouter router behaving erratically.

You can read more about the SVR.JS mod callbacks in the SVR.JS mod API documentation.

router.pass([path, ]callback)

Parameters:

  • path - the path (begins with "/"), for which the route applies. (optional, String)
  • callback - the SVR.JS mod callback, which the SVRouter router will pass to (Function)

Returns: the SVRouter router (so that you can chain the methods for routes or pass-throughs)

The function adds a pass-through (can be middleware) to the SVRouter router. The pass-through can be to an another SVRouter router (the request URLs relative to a parent route need to be provided for the router.route function in the SVRouter router).

The callback parameter has these arguments of the SVR.JS mod callback:

  • req - the SVR.JS request object
  • res - the SVR.JS response object
  • logFacilities - the SVR.JS log facilities object
  • config - the SVR.JS configuration object
  • next - the callback which passes the execution to other routes, SVR.JS mods and SVR.JS internal handlers.

The req object has an additional svrouterBase property, which contains the route base used internally by a SVRouter router. It's not recommended to override this property, as doing it may result in a SVRouter router behaving erratically.

You can read more about the SVR.JS mod callbacks in the SVR.JS mod API documentation.

router.passExpressRouterMiddleware([path, ]middleware)

Parameters:

  • path - the path (begins with "/"), for which the route applies. (optional, String)
  • middleware - the middleware compatible with the router library (Function)

Returns: the SVRouter router (so that you can chain the methods for routes or pass-throughs)

The function adds middleware compatible with the router library to the SVRouter router. Middleware that depends on Express's request and response properties may not work.

The middleware parameter has these arguments of middleware compatible with the router library:

  • req - the request object
  • res - the response object
  • next - the callback which passes the execution to other routes, SVR.JS mods and SVR.JS internal handlers.

router.get(path, callback)

An alias to the router.route("GET", path, callback) function

router.post(path, callback)

An alias to the router.route("POST", path, callback) function

router.put(path, callback)

An alias to the router.route("PUT", path, callback) function

router.patch(path, callback)

An alias to the router.route("PATCH", path, callback) function

router.delete(path, callback)

An alias to the router.route("DELETE", path, callback) function

router.head(path, callback)

An alias to the router.route("HEAD", path, callback) function

router.all(path, callback)

An alias to the router.route("*", path, callback) function