Verified Commit 1f9ec4ac authored by Aral Balkan's avatar Aral Balkan
Browse files

Improve example so it can run from any folder; update readme

parent d8731882
const express = require('express')
const path = require('path')
const express = require('express')
const getRoutes = require ('..')
const app = express()
const routes = getRoutes('.routes')
const routes = getRoutes(path.join(__dirname, '.routes'))
routes.forEach(route => {
console.log(` • Adding route: ${route.path}`)
......
......@@ -2,13 +2,13 @@
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
## Installation
```shell
npm i @ind.ie/web-routes-from-files
npm i @small-tech/web-routes-from-files
```
## Use
## Usage
```js
const getRoutes = require ('web-routes-from-files')
......@@ -51,21 +51,23 @@ You will get the following data structure:
]
```
Which, for example, you could pass to an [Express](https://expressjs.com/) app:
Which, for example, you could pass to an [Express](https://expressjs.com/) app (as [Site.js](https://sitejs.org)) does:
```js
const express = require('express')
const getRoutes = require ('web-routes-from-files')
const path = require('path')
const express = require('express')
const getRoutes = require ('..')
const app = express()
const routes = getRoutes('.routes')
const routes = getRoutes(path.join(__dirname, '.routes'))
routes.forEach(route => {
console.log(` • Adding route: ${route.path}`)
app.get(route.path, require(route.callback))
})
app.listen(8080, () => console.log('Server running on http://localhost:8080'))
app.listen(8080, () => console.log('\nServer running on http://localhost:8080'))
```
Your routes should export standard middleware-style functions. e.g.,
......@@ -77,6 +79,30 @@ function route (request, response, next) {
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).
This example is included in the source code. Run it with:
```sh
node example
```
__Note:__ This 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).
## Like this? Fund us!
[Small Technology Foundation](https://small-tech.org) is a tiny, independent not-for-profit.
We exist in part thanks to patronage by people like you. If you share [our vision](https://small-tech.org/about/#small-technology) and want to support our work, please [become a patron or donate to us](https://small-tech.org/fund-us) today and help us continue to exist.
## Audience
This is [small technology](https://small-tech.org/about/#small-technology).
If you’re evaluating this for a “startup” or an enterprise, let us save you some time: this is not the right tool for you. This tool is for individual developers to build personal web sites and apps for themselves and for others in a non-colonial manner that respects the human rights of the people who use them.
## Copyright
© 2020 [Aral Balkan](https://ar.al), [Small Technology Foundation](https://small-tech.org).
## License
__Copyright:__ ⓒ 2019 [Aral Balkan](https://ar.al). Licensed under AGPLv3 or later.
[AGPL version 3.0 or later.](https://www.gnu.org/licenses/agpl-3.0.en.html)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment