Authored by 郭成尧

cors-midd

  1 +const _ = require('lodash');
  2 +
  3 +/**
  4 + * 异步请求跨域处理
  5 + */
  6 +module.exports = (req, res, next) => {
  7 + let allowOrigin = _.get(req, 'headers.origin', null) ?
  8 + req.headers.origin : req.protocol + '://' + req.headers.host;
  9 +
  10 + res.setHeader('Access-Control-Allow-Origin', allowOrigin);
  11 + res.setHeader('Access-Control-Allow-Credentials', 'true');
  12 +
  13 + return next();
  14 +};