vuestorefront/vue-storefront

Extending express app in extendApp()

avareldev opened this issue · 1 comments

What is your question / Please describe your issue

Hello,
We are creating a custom integration that needs file uploads. Therefor we tried to add a custom extension to the integration and implement the extendApp() function like this:

extensions: (extensions) => [
    ...extensions,
    {
      name: 'upload-extension',
      extendApp: ({app, configuration}) => {
       app.use(express.json({limit: '50mb'}));
      },
    },
  ],

It seems that the app.use call does not have any effect here.

How can we set the size limit on the express application?

What version of Vue Storefront are you using?

2.7.1

Code of Conduct

  • I agree to follow this project's Code of Conduct
Fifciu commented

It should work perfectly fine. Probably the source of the issue is the thing we register express.json() middleware a few lines above in the core by default.

I am afraid the solution is replacing our out-of-the-box express.json() middleware. Probably you will need to access the internal _router.stack property. Here you can read more about it.

E.g.

const express = require('express');
//...

extensions: (extensions) => [
  ...extensions,
  {
    name: 'upload-extension',
    extendApp: ({ app }) => {
      const JSON_MIDDLEWARE_NAME = 'jsonParser'
      const jsonMiddleware = app._router.stack.find(
        (s) => s.name === JSON_MIDDLEWARE_NAME
      );
      jsonMiddleware.handle = express.json({ limit: '50mb' })
    },
  },
],