diff --git a/README.md b/README.md index 8ee5821..247fdb9 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ Parameters: 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 @@ -98,3 +100,6 @@ 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 \ No newline at end of file diff --git a/src/index.js b/src/index.js index e4d0d28..158769c 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ function svrouter() { } routes.push({ - method: method.toUpperCase(), + method: method === "*" ? null : method.toUpperCase(), pathFunction: match(path), callback: callback }); @@ -96,6 +96,7 @@ function svrouter() { addRoute(method, path, callback); }); + router.all = (path, callback) => addRoute("*", path, callback); router.route = addRoute; router.pass = passRoute; diff --git a/tests/index.test.js b/tests/index.test.js index 342b09f..00e67f2 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -49,6 +49,27 @@ describe("SVRouter", () => { expect(res.end).toHaveBeenCalledWith("POST route matched"); }); + test("should add and handle a route for all HTTP methods", () => { + const req = { + method: "GET", + parsedURL: { pathname: "/test" }, + params: null + }; + const res = { + end: jest.fn() + }; + + router.all("/test", (req, res) => { + res.end("GET route matched"); + }); + + router(req, res, null, null, () => { + res.end("No route matched"); + }); + + expect(res.end).toHaveBeenCalledWith("GET route matched"); + }); + test("should correctly parse parameters in the route", () => { const req = { method: "GET",