Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samuelnovaes/routerfy
Automatic route generator for Express
https://github.com/samuelnovaes/routerfy
automatic express generator middleware route router routerfy
Last synced: 5 days ago
JSON representation
Automatic route generator for Express
- Host: GitHub
- URL: https://github.com/samuelnovaes/routerfy
- Owner: samuelnovaes
- License: bsd-2-clause
- Created: 2018-04-29T17:27:50.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-11T19:06:08.000Z (about 4 years ago)
- Last Synced: 2024-10-14T07:57:25.213Z (about 1 month ago)
- Topics: automatic, express, generator, middleware, route, router, routerfy
- Language: JavaScript
- Size: 14.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# routerfy
Nuxt like router for Express
[![NPM](https://nodei.co/npm/routerfy.png)](https://nodei.co/npm/routerfy/)
# install
```bash
npm install routerfy
```# Usage
Routerfy automatically generates the routes based on your file tree inside the routes directory.
```javascript
const express = require('express')
const routerfy = require('routerfy')const app = express()
app.use(routerfy('routes'))
app.listen(8080)
```# Basic routes
This file tree:
```
routes/
--| user/
-----| index.js
-----| one.js
--| index.js
```will automatically generate:
- /
- /user
- /user/one# Dynamic routes
To define a dynamic route with a parameter, you need to define a JavaScript file OR a directory prefixed by an underscore.
This file tree:
```
routes/
--| _slug/
-----| comments.js
-----| index.js
--| users/
-----| _id.js
--| index.js
```will automatically generate:
- /
- /users/:id
- /:slug
- /:slug/comments> Note: For dynamic routes to work properly, you must use the `mergeParams: true`javascript option when calling the `express.Router` function
```javascript
const router = require('express').Router({ mergeParams: true });router.get('/', (req, res) => {
res.send(req.params.slug);
});module.exports = router;
```