A bracket in file name is actually represent directory
Last active
January 17, 2019 18:04
-
-
Save muhammadfaizan/97f61db6aaf7a229dda4baf6d882bd2f to your computer and use it in GitHub Desktop.
Creating dynamic routes in node.js (Express.js)
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
/* | |
* Copyright (c) 2019 Muhammad Faizan | |
* | |
* Author: Muhammad Faizan | |
*/ | |
const modules = require('./modules/index') | |
const routes = require('./routes/index') | |
module.exports = { | |
modules, | |
routes | |
} |
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 router = require('express').Router() | |
const v1 = require('./module-r1') | |
router.use('/v1', v1) | |
module.exports = 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 fs = require('fs') | |
let dirname = fs.readdirSync('./'); | |
dirname.forEach((name) => { | |
app.use(`/${name}`, require(`./${name}/index`).routes); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment