const koa = require("koa");const router = require("koa-router")();const app = new koa();const bodyParser = require("koa-bodyparser");router.post("/login", async ctx => { // username=cheng pass=123 var {username,pass} = ctx.request.body; if (username == "cheng" && pass == "123") { ctx.cookies.set("auth", true, { httpOnly: false }) ctx.body = { auth: true, code: 200, msg: "登录成功" } }else{ ctx.body = { code:400, msg:"登录失败" } }})app.use(async (ctx, next) => { ctx.set("Access-Control-Allow-Methods", "*") // 跨域 ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'); ctx.set("Access-Control-Allow-Origin", ctx.headers.origin); //允许跨域发送cookie请求 ctx.set("Access-Control-Allow-Credentials", true) if (ctx.method == "OPTIONS") { ctx.body = 200 } else { await next(); }})app.use(bodyParser());app.use(router.routes());app.listen(8000);