Search code examples
javascripthttpmiddlewaremiddy

How to write export using const in Javascript?


I am using the middy package from npm. I need someone to tell me how to write the following code using module.exports

export default handler => middy(handler)
  .use([
   httpBodyParser,
   httpEventNormalizer,
   httpErrorHandler
])

The function above is anonymous which takes a handler function and wraps the middleware around it. How to write this using module.exports? Please help me!!


Solution

  • "latest JavaScript" wouldn't use module.exports at all, it would use export as the code shippet shown does. If the question is really how to do this the old CommonJS way, you'd do it by assigning to module.exports directly:

    // Old CommonJS way
    module.exports = (handler) => middy(handler)
        .use([
          httpBodyParser,
          httpEventNormalizer,
          httpErrorHandler
       ]);
    

    But if possible, I'd use standard JavaScript modules ("ECMAScript Modules" aka "ESM" — i.e., export and import) instead.