Skip to content

Instantly share code, notes, and snippets.

@gagan-bansal
Last active July 30, 2022 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagan-bansal/c88aa4b7e6cc6684597834e745732785 to your computer and use it in GitHub Desktop.
Save gagan-bansal/c88aa4b7e6cc6684597834e745732785 to your computer and use it in GitHub Desktop.
With express mount routers route and list routes of router.
const express = require('express');
const app = express();
const router = express.Router();
router.get('/pets/cats', (req, res) => {
res.send('10 cats');
});
router.get('/pets/dogs', (req, res) => {
res.send('5 dogs');
});
router.post('/pets/dogs', (req, res) => {
res.send('A puppy added');
});
router.get('/wild/tigers', (req, res) => {
res.send('1 tiger');
});
app.use('/animals', (req, res, next) => {
// res.send('come later');
next();
}, router);
router.stack.forEach(route => {
Object.keys(route.route.methods).forEach(method => {
console.log('%s %s', method, route.route.path );
})
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment