vuestorefront / vue-storefront

Alokai is a Frontend as a Service solution that simplifies composable commerce. It connects all the technologies needed to build and deploy fast & scalable ecommerce frontends. It guides merchants to deliver exceptional customer experiences quickly and easily.
https://www.alokai.com
MIT License
10.59k stars 2.08k forks source link

Extending express app in extendApp() #6827

Closed avareldev closed 1 year ago

avareldev commented 1 year ago

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

Fifciu commented 1 year ago

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' })
    },
  },
],