
Security News
PodRocket Podcast: Inside the Recent npm Supply Chain Attacks
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
@jpbberry/load-routes
Advanced tools
@jpbberry/load-routes
is a package dedicated to easily, powerfully, and simply dynamically loading routes over folders and sub-folders. Routes are decides by the folders they're in, and the name of the files.
(This also has support for typescript, just replace all module.exports = ...
with exports default ...
)
Install via npm i @jpbberry/load-routes
The package exports LoadRoutes
with the paramaters LoadRoutes(app, directory, bind?)
The express app or router to load the directory into
A full path directory to load all routes in
An optional variable to bind all the routers function by, making it this
index.js
const { LoadRoutes } = require('@jpbberry/load-routes')
const Path = require('path')
const Express = require('express')
const app = Express()
LoadRoutes(app, Path.resolve(__dirname, './routes'))
app.listen(3000)
And in your /routes folder, put your routes!
routes/foo.js
module.exports = function (r) {
r.get('/', (req, res) => {
res.json({
bar: true
})
})
r.get('/not', (req, res) => {
res.json({
bar: false
})
})
}
When loaded, this will now make ip:3000/foo and /foo/not with these GETs!
Sometimes your middlewares and base / files can get pretty big, so you can add an index.js which will let you listen to the inside of the sub-folder, for example:
routes/hello/index.js
module.exports = function (r) {
r.get('/', (req, res) => {
res.json({
hello: 'world'
})
})
}
And then you can have your non-cluttered routes in side of the hello
folder
routes/hello/test.js
module.exports = function (r) {
r.get('/', (req, res) => {
res.json({
test: true
})
})
}
And this will now listen on ip:3000/hello and /hello/test
You can pass an object which will be the same everywhere to the third paramater bind
. This is extremely useful in a client centered program. It will define the this
in every router. For example
index.js
const Client = {
foo: 'bar'
}
LoadRoutes(app, dir, Client)
Now Client
will be available as this
Also note that () => {} cannot access this
, you must use function ()
to get it
routes/foo.js
module.exports = function (r) {
r.get('/', (req, res) => {
res.json({
foo: this.foo // "bar"
})
})
}
FAQs
A package dedicated to loading routes dynamically
The npm package @jpbberry/load-routes receives a total of 5 weekly downloads. As such, @jpbberry/load-routes popularity was classified as not popular.
We found that @jpbberry/load-routes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.