Skip to content

Commit d016a26

Browse files
committed
🎨 使用koa-helmet、设置跨域域名
1 parent f193dd7 commit d016a26

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

server/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const Koa = require('koa')
33
const cors = require('koa2-cors')
4+
const helmet = require('koa-helmet')
45
const Router = require('koa-router')
56
const bodyParser = require('koa-bodyparser')
67
const consola = require('consola')
@@ -13,16 +14,17 @@ const config = require('../nuxt.config.js')
1314
config.dev = app.env !== 'production'
1415

1516
function useMiddleware(){
17+
app.use(helmet())
1618
app.use(bodyParser())
1719
//设置全局返回头
1820
app.use(cors({
1921
origin: function(ctx) {
20-
return '*';//cors
22+
return 'http://localhost:8000'; //cors
2123
},
2224
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
2325
maxAge: 86400,
2426
credentials: true, // 允许携带头部验证信息
25-
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD'],
27+
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
2628
allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Token', 'X-Device-Id', 'X-Uid'],
2729
}))
2830
}

0 commit comments

Comments
 (0)