A router library for SVR.JS 4.x mods
Find a file
2025-01-01 18:14:52 +01:00
.husky chore: init 2025-01-01 18:09:15 +01:00
src chore: init 2025-01-01 18:09:15 +01:00
tests chore: init 2025-01-01 18:09:15 +01:00
.gitignore chore: init 2025-01-01 18:09:15 +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: init 2025-01-01 18:09:15 +01:00
package.json fix: correct the Git repository URL 2025-01-01 18:14:52 +01:00
prettier.config.js chore: init 2025-01-01 18:09:15 +01:00
README.md chore: init 2025-01-01 18:09:15 +01:00

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, 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)

The function adds a route to 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 params parameter, 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".

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, for which the route applies. (optional, String)
  • callback - the SVR.JS mod callback, which the SVRouter router will pass to (Function)

The function adds middleware to the SVRouter router. The middleware can be an another SVRouter router (the absolute request URLs 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.

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

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