I'm having a problem with koa js and middleware order.
Im' using multiple middlewares, Koa-router, formidable, koa-static-folder and one for setting headers.
Right now, with current order, when I'm uploading a file from the frontend to backend via an API, it will throw a CORS error, which means the headers are not there (so this middleware is not loaded properly).
When I move them around, something else will stop working.
Here is my code:
this.app.use(serve({rootDir: './uploads', rootPath: '/public/uploads/'}));
this.app.use(router.routes());
this.app.use(formidable());
this.app.use((ctx, next) => {
// Website you wish to allow to connect
ctx.set('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
ctx.set('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
ctx.set('Access-Control-Allow-Credentials', "true");
// Pass to next layer of middleware
});
Can someone explain to me how to determine the right order of the middleware?