https://github.com/chfern/express-route-group
🚦 NPM module to help express route grouping.
https://github.com/chfern/express-route-group
library nodejs production
Last synced: about 2 months ago
JSON representation
🚦 NPM module to help express route grouping.
- Host: GitHub
- URL: https://github.com/chfern/express-route-group
- Owner: chfern
- Created: 2018-11-30T06:05:52.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T07:59:25.000Z (over 3 years ago)
- Last Synced: 2025-09-07T08:21:04.688Z (10 months ago)
- Topics: library, nodejs, production
- Language: JavaScript
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# express-route-group
This package enables express users to create a seperate file for their routes, and group routes by prefix.
# Installation
This is a node module and is available for install through npm
```
npm install express-route-group
```
# Usage
### index.js - Basic express configuration
Here, you create a basic express configuration then calling the function ```registerRoutes``` from routes.js (example below).
We pass the application instance returned from calling ```express()``` here.
```javascript
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Registers routes for your application
const {registerRoutes} = require('./routes');
registerRoutes(app);
const PORT = process.env.PORT || 8081;
app.listen(PORT, function(){
console.log(`Server starting on port ${PORT}`);
})
```
### routes.js
You may declare your route groups with these functions:
```
app.use(prefix, routes_arr)
```
```
app.use(prefix, middlewares_arr, routes_arr)
```
And to register the routes inside a route group, you can use these functions:
```
Route.get(endpoint, callback)
```
```
Route.post(endpoint, callback)
```
```
Route.put(endpoint, callback)
```
```
Route.patch(endpoint, callback)
```
```
Route.delete(endpoint, callback)
```
Example :
```javascript
const Route = require('express-route-group');
module.exports.registerRoutes = function(app) {
/*
* Register your routes here
* app.use(prefix, routes_arr)
* app.use(prefix, middlewares_arr, routes_arr)
*/
app.use('/test',
Route.routes([
Route.get('/helloworld', async (req, res, next) => {
res.send({ 'message': 'Hello World!' });
})
]));
}
```
Try starting the webserver then go to localhost:8081/test/helloworld, you should see the JSON returned.
### Registering Multiple Routes & Route Groups (routes.js)
```javascript
const Route = require('express-route-group');
module.exports.registerRoutes = function(app) {
app.use('/v1',
Route.routes([
Route.get('/todos', async (req, res, next) => {
res.send({ 'message': 'Hello World!' });
})
]));
app.use('/v2',
Route.routes([
Route.get('/todos', async (req, res, next) => {
res.send({ 'message': 'Hello World!' });
}),
Route.get('/todos/:id', async (req, res, next) => {
res.send({ 'message': 'Hello World!' });
})
]));
}
```