core/lib/middleware/credentials.js

38 lines
1.0 KiB
JavaScript

/**
*
* @authors yutent (yutent@doui.cc)
* @date 2018-09-03 22:26:51
*/
'use strict'
module.exports = function(req, res, next) {
var supportCredentials = this.get('supportCredentials')
var credentialsRule = this.get('credentialsRule')
var credentialsMaxAge = this.get('credentialsMaxAge')
if (supportCredentials) {
var origin = req.header('origin') || req.header('referer') || ''
var headers = req.header('access-control-request-headers')
origin = Util.url.parse(origin)
if (credentialsRule && origin.hostname) {
if (!credentialsRule.test(origin.hostname)) {
return res.end('')
}
}
res.set('Access-Control-Allow-Credentials', 'true')
res.set('Access-Control-Allow-Origin', `${origin.protocol}//${origin.host}`)
if (headers) {
res.set('Access-Control-Allow-Headers', headers)
}
if (credentialsMaxAge) {
res.set('Access-Control-Max-Age', credentialsMaxAge)
}
if (req.method === 'OPTIONS') {
return res.end('')
}
}
next()
}
一个轻量级的,易学的,拓展性灵活的 nodejs MVC 框架, 5 分钟即可上手。取自"Give me five"之意, 一切就是这么简单
JavaScript 100%