Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vinayak25/easy-router
A declarative router library for express.js.
https://github.com/vinayak25/easy-router
declarative-router express-router expressjs expressjs-router nodejs router routing
Last synced: about 1 month ago
JSON representation
A declarative router library for express.js.
- Host: GitHub
- URL: https://github.com/vinayak25/easy-router
- Owner: vinayak25
- License: mit
- Created: 2020-04-05T08:42:57.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T09:33:03.000Z (over 4 years ago)
- Last Synced: 2024-10-12T05:08:01.265Z (2 months ago)
- Topics: declarative-router, express-router, expressjs, expressjs-router, nodejs, router, routing
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Easy Router
![img](https://hanalabs-public-assets.s3.ap-south-1.amazonaws.com/easy-router/cover.png)
A declarative router library for express.js.
## Installation
Use the npm or yarn package manager to install the package.
```bash
#npm
npm i @vinayak2506/easy-router#yarn
yarn add @vinayak2506/easy-router
```## Usage
Start your expressjs server in `index.js`
```javascript
// index.jsconst express = require('express')
const app = express()
const port = 3000app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
```Now import @vinayak2506/easy-router
```javascript
// index.js// ...
const EasyRouter = require('@vinayak2506/easy-router')
```This will expose `EasyRouter` class which we will use in our router declarations.
```javascript
// index.js// ...
let router = new EasyRouter(express.Router())
```Now, we can use the router instance to declare our routes
```javascript
// METHOD : GET
router.get('/fb-post', function(req, res) {
// do your action
return res.send(200).json({})
})// METHOD : POST
router.post('/fb-post', function(req, res) {
// do your action
return res.send(200).json({})
})// METHOD : PUT
router.put('/fb-post', function(req, res) {
// do your action
return res.send(200).json({})
})// METHOD : PATCH
router.patch('/fb-post', function(req, res) {
// do your action
return res.send(200).json({})
})// METHOD : DELETE
router.delete('/fb-post', function(req, res) {
// do your action
return res.send(200).json({})
})
```Currently, the library only supports `GET, POST, PUT, PATCH, DELETE` HTTP methods.
### Grouping
We can **group** routes, see below for example
```javascript
router.group({prefix:'/fb-post'}, function() {
router.get('/', function(req, res) {
// do your stuff
})
router.post('/', function(req, res) {
// do your stuff
})
})
```### Middlewares
You need to register the middlewares using `registerMiddleware` before start using it.
```javascript
// single middleware registration
router.registerMiddleware('auth:jwt', JwtAuthenticationMiddleware)// multiple middleware registration
router.registerMiddleware({
'cors': CorsMiddleware,
'csrf': CsrfMiddleware,
'auth:jwt': JwtAuthenticationMiddleware,
'auth:session': SessionAuthenticationMiddleware,
})
```You can also group middlewares like below.
``` javascript
router.registerMiddleware({
web: [ 'cors', 'csrf', 'auth:session', ],
api: [ 'auth:jwt', ],
})
```You can register the middleware anywhere, but it is always recommended to do it in the top before declaring the routes.
To use the middleware, you just need to pass the names.
``` javascript
// in group method
router.group({prefix:'/fb-post', middlewares: ['auth:jwt']}, function() {
router.get('/', function(req, res) {
// do your stuff
})
router.post('/', function(req, res) {
// do your stuff
})
})
```## Security
If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## License
The MIT License. Please see LICENSE.md for more information.