Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpbberry/load-routes
A package dedicated to loading routes dynamically
https://github.com/jpbberry/load-routes
Last synced: about 1 month ago
JSON representation
A package dedicated to loading routes dynamically
- Host: GitHub
- URL: https://github.com/jpbberry/load-routes
- Owner: jpbberry
- Created: 2021-05-06T03:16:54.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-06T03:59:14.000Z (over 3 years ago)
- Last Synced: 2024-11-16T02:48:07.017Z (about 2 months ago)
- Language: TypeScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Load Routes
`@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 ...`)
## Installation
Install via `npm i @jpbberry/load-routes`
## How to
The package exports `LoadRoutes` with the paramaters `LoadRoutes(app, directory, bind?)`
#### Paramaters
- appThe express app or router to load the directory into
- directoryA full path directory to load all routes in
- [bind](#binding-routers)An optional variable to bind all the routers function by, making it `this`
### How to load a folder
**index.js**
```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**
```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!### Middlewares and indexes
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**
```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**
```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## Binding routers
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**
```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**
```js
module.exports = function (r) {
r.get('/', (req, res) => {
res.json({
foo: this.foo // "bar"
})
})
}
```## Links
- [GitHub](https://github.com/jpbberry/load-routes)
- [NPM](https://npmjs.com/packages/@jpbberry/load-routes)