Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

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

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ind.ie/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.

latest
npmnpm
Version
1.2.1
Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 29 Sep 2019

Did you know?

Socket

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.

Install

Related posts