Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 1.44 KB

README.md

File metadata and controls

59 lines (40 loc) · 1.44 KB

Express Router Extended

Add extra functionalities to express router, such as routing and middleware grouping, based on Laravel's router builder.

Install

NPM

$ npm install express-router-extended

Yarn

$ yarn add express-router-extended

How to Use

import express, { Response, Request } from 'express';
import ExpressRouterExtended from 'express-router-extended';

const expressInstance = express()
const router = ExpressRouterExtended.build()

let visits = 0;
const visitsMiddleware = (_req: Request, response: Response, next: () => void) => {
    console.log(`This page was accessed ${++visits} time(s)`)
    next()
}

router.group({ prefix: '/prefix', middlewares: [visitsMiddleware] }, router => {
    router.get('/route', () => console.log('Hello from /prefix/route using visitsMiddleware'))
    router.get('/route2', () => console.log('Hello from /prefix/route2 using visitsMiddleware'))

    router.group({prefix: '/second-prefix'}, router => {
        router.get('/route', () => console.log('Hello from /prefix/second-prefix/route using visitsMiddleware'))
    })
})

expressInstance.listen('8080', () => console.log('Running on Port 8080'))
expressInstance.use(router.exportRouter())

How to Test

$ npm run test or yarn test

Contributing

This project is open for pull requests. For major changes or discussions, please open an issue first.

License

MIT