A router library for SVR.JS 4.x mods
Find a file
2025-01-04 09:33:36 +01:00
.github/workflows chore: rename SVRouter to SVRRouter due to npm publishing issues 2025-01-04 09:32:18 +01:00
.husky chore: init 2025-01-01 18:09:15 +01:00
src chore: rename SVRouter to SVRRouter due to npm publishing issues 2025-01-04 09:32:18 +01:00
tests chore: rename SVRouter to SVRRouter due to npm publishing issues 2025-01-04 09:32:18 +01:00
.gitignore chore: ignore more files 2025-01-02 10:52:54 +01:00
.swcrc chore: init 2025-01-01 18:09:15 +01:00
commitlint.config.js chore: init 2025-01-01 18:09:15 +01:00
eslint.config.js chore: init 2025-01-01 18:09:15 +01:00
jest.config.js chore: init 2025-01-01 18:09:15 +01:00
LICENSE chore: init 2025-01-01 18:09:15 +01:00
lint-staged.config.js chore: init 2025-01-01 18:09:15 +01:00
package-lock.json chore: rename SVRouter to SVRRouter due to npm publishing issues 2025-01-04 09:32:18 +01:00
package.json chore: release SVRRouter 1.0.0 2025-01-04 09:33:36 +01:00
prettier.config.js chore: init 2025-01-01 18:09:15 +01:00
README.md chore: rename SVRouter to SVRRouter due to npm publishing issues 2025-01-04 09:32:18 +01:00

SVRRouter

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

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

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

// 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 SVRRouter",
  version: "0.0.0"
};

Methods (exported by the svrrouter package)

svrrouter()

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

Methods (provided by the SVRRouter 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 SVRRouter router (so that you can chain the methods for adding routes or pass-throughs)

The function adds a route to the SVRRouter 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 svrrouterBase property, which contains the route base used internally by a SVRRouter router. It's not recommended to override this property, as doing it may result in a SVRRouter 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 SVRRouter router will pass to (Function)

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

The function adds a pass-through (can be middleware) to the SVRRouter router. The pass-through can be to an another SVRRouter router (the request URLs relative to a parent route need to be provided for the router.route function in the SVRRouter 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 svrrouterBase property, which contains the route base used internally by a SVRRouter router. It's not recommended to override this property, as doing it may result in a SVRRouter 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 SVRRouter router (so that you can chain the methods for routes or pass-throughs)

The function adds middleware compatible with the router library to the SVRRouter 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