diff --git a/app.js b/app.js index b29d97e..e05f8f4 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,48 @@ dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/myblog',"wecatdemo" // console.log(requter) // import requter from "./bin/router" const app = new Koa(); +app.use(async (ctx, next) => { + // 允许来自所有域名请求 + ctx.set("Access-Control-Allow-Origin", "*"); + // 这样就能只允许 http://localhost:8080 这个域名的请求了 + // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); + + // 设置所允许的HTTP请求方法 + ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE"); + + // 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段. + ctx.set("Access-Control-Allow-Headers", "x-requested-with, accept, origin, content-type"); + + // 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。 + + // Content-Type表示具体请求中的媒体类型信息 + ctx.set("Content-Type", "application/json;charset=utf-8"); + + // 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。 + // 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*"; + ctx.set("Access-Control-Allow-Credentials", true); + + // 该字段可选,用来指定本次预检请求的有效期,单位为秒。 + // 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证 + // 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证 + ctx.set("Access-Control-Max-Age", 300); + + /* + CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段: + Cache-Control、 + Content-Language、 + Content-Type、 + Expires、 + Last-Modified、 + Pragma。 + */ + // 需要获取其他字段时,使用Access-Control-Expose-Headers, + // getResponseHeader('myData')可以返回我们所需的值 + //https://www.rails365.net/articles/cors-jin-jie-expose-headers-wu + ctx.set("Access-Control-Expose-Headers", "myData"); + + await next(); +}) app.use(async (ctx,next)=>{ // await next(); console.log(ctx.url) @@ -19,5 +61,5 @@ const bodyParser = require('koa-bodyparser'); app.use(bodyParser()); // console.log(requter()) app.use(requter()); -app.listen(3002); -console.log("http://localhost:3002") \ No newline at end of file +app.listen(9001); +console.log("http://localhost:9001") \ No newline at end of file diff --git a/controllers/index.js b/controllers/index.js index 2cf9dc0..db2074b 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -1,7 +1,8 @@ let index = async (ctx,next)=>{ - ctx.body = "" + console.log(ctx.request.body) + ctx.body = "ok" } module.exports = { - "GET /":index + "POST /":index } \ No newline at end of file