Repository: delvedor/find-my-way (js)

Issue:
TypeError: currentNode.createStaticChild is not a function for wildcard route
Hi,

Let's say I have the following code:
```js
const http = require("http");
const router = require("find-my-way")();

router.on("GET", "*.js", (req, res, params) => {
  res.end(`{"route":"${req.url}"}`);
});

const server = http.createServer((req, res) => {
  router.lookup(req, res);
});

server.listen(3000, (err) => {
  if (err) throw err;
  console.log("Server listening on: http://localhost:3000");
});
```

With find-my-way@4.5.1 (that is included in fastify@3.29.2) the code above works fine and I can run `curl http://localhost:3000/assets/hello.js` and get back `{"route":"/assets/hello.js"}` but with `find-my-way@7.2.0` (that is included in fastify@4.6.0) I get:

```
TypeError: currentNode.createStaticChild is not a function
    at Router._on (node_modules/find-my-way/index.js:190:33)
    at Router.on (node_modules/find-my-way/index.js:136:10)
    at Object.<anonymous> (server.cjs:20:8)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
```

I guess this is a bug? Or is it a better way to get all requests that request for a javascript file?

Pull request description:
Add error for wildcard route with suffix
Close #304 https://github.com/fastify/fastify/issues/4306

Patch:
diff --git a/index.js b/index.js
index 32fd008..551c2cc 100644
--- a/index.js
+++ b/index.js
@@ -267,6 +267,10 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
       params.push('*')
       currentNode = currentNode.createWildcardChild()
       parentNodePathIndex = i + 1
+
+      if (i !== path.length - 1) {
+        throw new Error('Wildcard must be the last character in the route')
+      }
     }
   }
 


Test patch:
diff --git a/test/issue-28.test.js b/test/issue-28.test.js
index fb901e6..c25d3ef 100644
--- a/test/issue-28.test.js
+++ b/test/issue-28.test.js
@@ -601,3 +601,19 @@ test('Wildcard node with constraints', t => {
     null
   )
 })
+
+test('Wildcard must be the last character in the route', (t) => {
+  t.plan(6)
+
+  const expectedError = new Error('Wildcard must be the last character in the route')
+
+  const findMyWay = FindMyWay()
+
+  t.throws(() => findMyWay.on('GET', '*1', () => {}), expectedError)
+  t.throws(() => findMyWay.on('GET', '*/', () => {}), expectedError)
+  t.throws(() => findMyWay.on('GET', '*?', () => {}), expectedError)
+
+  t.throws(() => findMyWay.on('GET', '/foo*123', () => {}), expectedError)
+  t.throws(() => findMyWay.on('GET', '/foo*?', () => {}), expectedError)
+  t.throws(() => findMyWay.on('GET', '/foo*/', () => {}), expectedError)
+})