配置了cors后却提示这个报错,是怎么回事?怎么处理跨域

来源:4-3 图形验证码功能开发1

雷灵初心

2021-04-06 18:47:12

配置了cors后却提示这个报错,是怎么回事?怎么处理跨域

// const koa = require('koa')
// const path = require('path') // node的path模块,用于处理文件路径
import koa from 'koa'
import path from 'path'
import helmet from 'koa-helmet'
import statics from 'koa-static'
import router from './routes/routes'
import cors from 'koa2-cors' //跨域处理
const app = new koa()
// const helmet = require('koa-helmet') // 安全头盔
// const statics = require('koa-static') // 静态文件插件

// const router = require('./routes/routes')


app.use(helmet())
app.use(statics(path.join(__dirname, '../public')))
app.use(router())
app.use(
cors({
origin: function (ctx) { //设置允许来自指定域名请求
if (ctx.url === '/') {
return '*'; // 允许来自所有域名请求
}
return 'http://localhost:8080'; //只允许http://localhost:8080这个域名的请求
},
maxAge: 5, //指定本次预检请求的有效期,单位为秒。
credentials: true, //是否允许发送Cookie
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
})
)

app.listen(3000)

http://img.mukewang.com/climg/606c3c200967780011750886.jpg

写回答

1回答

Brian

2021-04-07

0

0 学习 · 1842 问题

查看课程