generic-import
Iterable, recursive, generic, cumulative imports.
Usage
import genericImport from "generic-import";
...express backend initial code
interface MyRouterInterface {
basepath: string,
router: Router // imported from express
}
const routesPath = path.join(process.cwd(), "routes");
const routers = genericImport<MyRouterInterface>(routesPath);
// [
// { filename: "router1", value: Object },
// { filename: "router2", value: Object },
// ]
for(const r of routers){ // for of loop supported in only ES syntax
app.use(r.value.basepath, r.value.router);
}
app.listen(8080);
It provides no coupling between main script file and route files. So there is much more scalable code. Why use this approach?
Options
- camelize boolean Camelizes returned file names of modules
- fileExtensions array Dot prefixed file extensions to include in search
- recursive boolean Enables search in nested folders