Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECMAScript modules example #39

Open
brandonros opened this issue Dec 11, 2019 · 3 comments
Open

ECMAScript modules example #39

brandonros opened this issue Dec 11, 2019 · 3 comments

Comments

@brandonros
Copy link

Might make sense to put one in the README?

@jorgegonzalez
Copy link

Does this package work with the Node 14 ESM implementation at all?

@brandonros
Copy link
Author

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-s96
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants