A router library for SVR.JS 4.x mods
Find a file
2025-01-02 11:34:40 +01:00
.husky chore: init 2025-01-01 18:09:15 +01:00
src fix: fix the bug with "path.replace" not being a function when adding middleware for Express router 2025-01-01 22:06:44 +01:00
tests feat: add compatiblity with Express's router middleware 2025-01-01 21:55:32 +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: downgrade path-to-regexp library to version compatible with older Node.JS versions 2025-01-02 11:34:40 +01:00
package.json chore: downgrade path-to-regexp library to version compatible with older Node.JS versions 2025-01-02 11:34:40 +01:00
prettier.config.js chore: init 2025-01-01 18:09:15 +01:00
README.md docs: add note about some Express middleware not working 2025-01-01 22:10:35 +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 (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 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 (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 absolute request URLs need to be provided for the router.route function in the SVRouter router). Note that the request URL is not rewritten though, unlike in the router library, so if you write middleware for SVRouter, you may need to include the request URL prefix in the parameters of the function that returns the SVR.JS mod callback.

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.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