Last active
January 17, 2021 11:24
-
-
Save onhate/53ebabb8c390fd0c9404cf9ed9c0f4ee to your computer and use it in GitHub Desktop.
Load Express Routes dynamically like Nuxt.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
const express = require('express'); | |
const glob = require("glob"); | |
const app = express(); | |
glob.sync('./api/**/*.http.js', { cwd: __dirname }) | |
.forEach(file => { | |
const endpoint = file.split('.http.js').join('') | |
.split('_').join(':') | |
.substr(1); | |
app.use(endpoint, require(file)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This small snippet of code converts this file structure:
into
and a
.http.js
will look like this, from the file perspective it binds to/
as the path to it will be the express route path bind to the router.