This commit is contained in:
binaryify
2018-01-12 14:47:31 +08:00
parent 480b1696eb
commit 642d6f5353
7 changed files with 608 additions and 654 deletions
+12 -11
View File
@@ -6,17 +6,18 @@ const app = express();
let cache = apicache.middleware;
// 跨域设置
// app.all('*', function (req, res, next) {
// if (req.path !== '/' && !req.path.includes('.')) {
// res.header('Access-Control-Allow-Credentials', true)
// // 这里获取 origin 请求头 而不是用 *
// res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
// res.header('Access-Control-Allow-Headers', 'X-Requested-With')
// res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
// res.header('Content-Type', 'application/json;charset=utf-8')
// }
// next()
// })
app.all("*", function(req, res, next) {
if (req.path !== "/" && !req.path.includes(".")) {
res.header("Access-Control-Allow-Credentials", true);
// 这里获取 origin 请求头 而不是用 *
res.header("Access-Control-Allow-Origin", req.headers["origin"] || "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Content-Type", "application/json;charset=utf-8");
}
next();
});
const onlyStatus200 = (req, res) => res.statusCode === 200;
app.use(cache("2 minutes", onlyStatus200));