ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 跨域支持 API访问经常会碰到跨域问题,Thinkjs通过中间件非常简单的实现跨域支持。 修改/config/middleware.js增加以下内容 ~~~ const cors = require("@koa/cors"); { handle: cors, }, ~~~ 记住要安装模块 ~~~ cnpm i @koa/cors --save ~~~ 文件路径: /config/middleware.js ~~~ const path = require("path"); const isDev = think.env === "development"; const cors = require("@koa/cors"); module.exports = [ { handle: "meta", options: { logRequest: isDev, sendResponseTime: isDev, }, }, { handle: cors, }, { handle: "resource", enable: isDev, options: { root: path.join(think.ROOT_PATH, "www"), publicPath: /^\/(static|favicon\.ico)/, }, }, { handle: "trace", enable: !think.isCli, options: { debug: isDev, }, }, { handle: "payload", options: { keepExtensions: true, limit: "5mb", }, }, { handle: "router", options: {}, }, "logic", "controller", ]; ~~~