ECMAScript modules example
brandonros opened this issue · comments
Brandon Ros commented
Might make sense to put one in the README?
Jorge Gonzalez commented
Does this package work with the Node 14 ESM implementation at all?
Brandon Ros commented
export const initRoutes = async (app) => {
const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const filenames = glob.sync(path.resolve(__dirname + '/../routes/**/*.mjs'))
for (let i = 0; i < filenames.length; ++i) {
const filename = filenames[i]
const { default: route } = await import(filename)
app[route.method](
route.path,
buildRequestIdMiddleware(),
buildRequestLoggerMiddleware(),
buildRateLimitMiddleware(route),
buildAsyncRouteHandlerMiddleware(route),
buildResponseLoggerMiddleware()
)
}
}
probably not. i abandoned trying to use it it and did this.
Mohit Srivastava commented
I came across this issue today where I wanted to use ES6 imports which this package doesn't support.
My use case was initializing routes/middlewares on app start. So I made a simple alternative that lets me use the visit and include options like this package but with ESM. Works well with top-level await.
Here's an example