1 Star 1 Fork 1

QuickSpeed/h5-Dooring

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.js 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
yehuozhili 提交于 2020-09-15 16:00 . feat autosave&optimize ide
const Koa = require('koa');
const { resolve } = require('path');
const staticServer = require('koa-static');
const koaBody = require('koa-body');
const cors = require('koa2-cors');
const logger = require('koa-logger');
const app = new Koa();
app.use(staticServer(resolve(__dirname, './static')));
app.use(koaBody());
app.use(logger());
// 设置跨域
app.use(
cors({
origin: function(ctx) {
if (ctx.url.indexOf('/dooring') > -1) {
return '*'; // 允许来自所有域名请求
}
return '';
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization', 'x-test-code'],
maxAge: 5, // 该字段可选,用来指定本次预检请求的有效期,单位为秒
credentials: true,
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowHeaders: [
'Content-Type',
'Authorization',
'Accept',
'x-requested-with',
'Content-Encoding',
],
}),
);
let htmlStr = '';
app.use(async (ctx, next) => {
console.log(ctx.url);
if (ctx.url === '/dooring/render') {
htmlStr = ctx.request.body;
ctx.body = 'success';
} else if (ctx.url.indexOf('/html') === 0) {
ctx.type = 'html';
ctx.body = htmlStr;
}
});
app.listen(3000);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quick-speed/h5-Dooring.git
git@gitee.com:quick-speed/h5-Dooring.git
quick-speed
h5-Dooring
h5-Dooring
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385