Last active
July 30, 2022 13:46
-
-
Save gagan-bansal/c88aa4b7e6cc6684597834e745732785 to your computer and use it in GitHub Desktop.
With express mount routers route and list routes of router.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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