This package has been deprecated

Author message:

Future development continues at @small-tech/web-routes-from-files (https://source.small-tech.org/site.js/lib/web-routes-from-files).

@ind.ie/web-routes-from-files

1.2.1 • Public • Published

Web routes from files

Recursively traverses a given directory structure and uses convention to create a list of web route objects that map url paths to JavaScript callback files.

Install

npm i @ind.ie/web-routes-from-files

Use

const getRoutes = require ('web-routes-from-files')

const routes = getRoutes('.')

routes.forEach(route => {
  console.log(`${route.path}: ${route.callback}`)
})

Details

Given the following directory structure:

.routes
   ├─── index.js
   ├─── my-folder
   │       ├── index.js
   │       └── other.js
   └─── neat.js

And the following invocation:

const getRoutes = require ('web-routes-from-files')
const routes = getRoutes('.routes')

You will get the following data structure:

[
  { path: '/', file: '.routes/index.js' },
  { path: '/my-folder', file: '.routes/my-folder/index.js' },
  { path: '/my-folder/other', file: '.routes/my-folder/other.js' },
  { path: '/neat', file: '.routes/neat.js' }
]

Which, for example, you could pass to an Express app:

const express = require('express')
const getRoutes = require ('web-routes-from-files')

const app = express()
const routes = getRoutes('.routes')

routes.forEach(route => {
  app.get(route.path, require(route.callback))
})

app.listen(8080, () => console.log('Server running on http://localhost:8080'))

Your routes should export standard middleware-style functions. e.g.,

function route (request, response, next) {
  response.end('Hello, world!')
}
module.exports = route

Note: The module will ignore node_modules folders and any folder within the root folder being traversed that begins with a dot (i.e., any hidden folder).

Copyright: ⓒ 2019 Aral Balkan. Licensed under AGPLv3 or later.

Readme

Keywords

none

Package Sidebar

Install

npm i @ind.ie/web-routes-from-files

Weekly Downloads

2

Version

1.2.1

License

AGPL-3.0-or-later

Unpacked Size

40.3 kB

Total Files

11

Last publish

Collaborators

  • aral