Skip to content

Instantly share code, notes, and snippets.

@muhammadfaizan
Last active January 17, 2019 18:04
Show Gist options
  • Save muhammadfaizan/97f61db6aaf7a229dda4baf6d882bd2f to your computer and use it in GitHub Desktop.
Save muhammadfaizan/97f61db6aaf7a229dda4baf6d882bd2f to your computer and use it in GitHub Desktop.
Creating dynamic routes in node.js (Express.js)
/*
* Copyright (c) 2019 Muhammad Faizan
*
* Author: Muhammad Faizan
*/
const modules = require('./modules/index')
const routes = require('./routes/index')
module.exports = {
modules,
routes
}
const router = require('express').Router()
const v1 = require('./module-r1')
router.use('/v1', v1)
module.exports = router

A bracket in file name is actually represent directory

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