Skip to content

Commit

Permalink
0.23.1
Browse files Browse the repository at this point in the history
- Added feature that scans the router stack to move the 404 route (the `*` route) to the end of the stack every time a new route is added, even if the route is added at runtime so that you can dynamically add routes while the app is running.
- Updated various dependencies.
  • Loading branch information
kethinov committed Sep 10, 2024
1 parent 66d95b1 commit 8a7338a
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 96 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

- Put your changes here...

## 0.23.1

- Added feature that scans the router stack to move the 404 route (the `*` route) to the end of the stack every time a new route is added, even if the route is added at runtime so that you can dynamically add routes while the app is running.
- Updated various dependencies.

## 0.23.0

- Removed `cores` feature since it is largely redundant now thanks to the widespread popularity of tools like pm2. Also removed various deprecated cluster module support as well.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,14 @@ Here's how to set up a development environment to hack on Roosevelt's code:
If some of the automated tests fail for you when they shouldn't be, make sure you remove the `test/app` folder and kill any Node.js processes (e.g. `killall node`) before running the test suite again.
If you want to see the output from a generated test app in one of the tests, insert this block of code into the test:
```javascript
testApp.stdout.on('data', (data) => {
console.log(data.toString())
})
```
#### Support Roosevelt's development
You can support Roosevelt's development and maintenance by [buying merch](https://curly-braces-merch.printify.me/products) or [donating](https://www.paypal.com/donate/?hosted_button_id=2L2X8GRXZCGJ6). Please note that donations are not tax-deductible. Thank you for your support!
31 changes: 29 additions & 2 deletions lib/mapRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,40 @@ module.exports = app => {
// load 404 controller last so that it doesn't supersede the others
try {
require(params.errorPages.notFound)(router, app)

// define a function to move the 404 controller (the "*" route) to the end
function moveWildcardRouteToEnd (stack) {
const wildcardIndex = stack.findIndex(layer => layer.route && layer.route.path === '*')
if (wildcardIndex !== -1) {
const [wildcardRoute] = stack.splice(wildcardIndex, 1)
stack.push(wildcardRoute)
}
}

// create a proxy to observe changes to router.stack
const stackProxy = new Proxy(router.stack, {
set (target, property, value) {
target[property] = value
moveWildcardRouteToEnd(target)
return true
},
deleteProperty (target, property) {
delete target[property]
moveWildcardRouteToEnd(target)
return true
}
})

// replace router.stack with the proxy
router.stack = stackProxy

// note there is a simlar object located at app._router.stack with the app's routes instead of the router's routes that we're not modifying
// this is because it seems app._router.stack routes (e.g. /reloadHttp/reload.js and /reloadHttps/reload.js) take precedence over any routes in router.stack
} catch (e) {
logger.error(`${appName} failed to load 404 controller file: ${params.errorPages.notFound}. Please make sure it is coded correctly. See documentation at http://github.com/rooseveltframework/roosevelt for examples.`)
logger.error(e)
}

// TODO: consider adding an object observer to move the 404 controller to the end of the stack anytime the router stack is mutated

// activate the router module
app.use(prefix || '/', router)

Expand Down
Loading

0 comments on commit 8a7338a

Please sign in to comment.