# Separate Middleware

Along with the Vue Storefront architecture you can create separate API Middleware. To do this you can use our internal express.js instance or pass your own server connection. To connect with our internal instance you need to create a special endpoint file to handle this.

// middleware.js

const { createServer } = require('@vue-storefront/middleware');
const { integrations } = require('./middleware.config');
const cors = require('cors');

(async () => {
  const app = await createServer({ integrations });
  const CORS_MIDDLEWARE_NAME = 'corsMiddleware';

  /**
   * Temporarily overwrite cors settings to allow credentials exchange between
   * localhost:3000 and locahost:8181
   */
  const corsMiddleware = app._router.stack.find(
    (s) => s.name === CORS_MIDDLEWARE_NAME
  );
  corsMiddleware.handle = cors({
    origin: [
      'http://localhost:3000'
      // Here you can add urls to your storefronts in case of using multi-storefront feature.
    ],
    credentials: true
  });

  app.listen(8181, () => {
    console.log('BigCommerce middleware started');
  });
})();

This way your server connection will be exposed on the 8181 port.

Now you can use it (port and host) within the nuxt.config. Like this.

{
  ...,
  publicRuntimeConfig: {
    middlewareUrl:
      process.env.NODE_ENV === 'production'
        ? process.env.API_BASE_URL
        : 'http://localhost:8181/'
  },
}

Remember to run your server at the same time along with your web application.

node middleware.js

You can add new scripts to the package.json file as well, and run processes concurrently.

{
  "scripts": {
    "dev": "concurrently \"nuxt\" \"yarn start:middleware\"",
    "middleware": "node middleware.js"
  }
}

Of course, you can create your own middleware connection and pass to it your own server that will handle your API calls. This is just an example.