Skip to content

Commit 6821a3e

Browse files
author
Ido Shamun
committed
fix: cors issue
1 parent d44b008 commit 6821a3e

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import KnexStore from 'koa-generic-session-knex';
88
import userAgent from 'koa-useragent';
99
import etag from 'koa-etag';
1010
import views from 'koa-views';
11+
import cors from '@koa/cors';
1112

1213
import config from './config';
1314
import errorHandler from './middlewares/errorHandler';
@@ -34,6 +35,16 @@ app.keys = [config.cookies.key];
3435

3536
app.proxy = config.env === 'production';
3637

38+
app.use(cors({
39+
credentials: true,
40+
origin(ctx) {
41+
const requestOrigin = ctx.get('Origin');
42+
if (config.cors.origin.indexOf(requestOrigin) > -1) {
43+
return requestOrigin;
44+
}
45+
return false;
46+
},
47+
}));
3748
app.use(bodyParser());
3849
app.use(KoaPinoLogger({ logger }));
3950
app.use(errorHandler());

src/middlewares/cors.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import cors from '@koa/cors';
21
import config from '../config';
32

4-
const corsMiddleware = cors({
5-
credentials: true,
6-
origin(ctx) {
7-
const requestOrigin = ctx.get('Origin');
8-
if (config.cors.origin.indexOf(requestOrigin) > -1) {
9-
return requestOrigin;
10-
}
11-
return false;
12-
},
13-
});
14-
153
export default (ctx, next) => {
164
if (config.env === 'test') {
175
return next();
@@ -24,5 +12,5 @@ export default (ctx, next) => {
2412
return Promise.resolve();
2513
}
2614

27-
return corsMiddleware(ctx, next);
15+
return next();
2816
};

0 commit comments

Comments
 (0)