v0.0.7 完善对跨域的处理

This commit is contained in:
yi-ge 2017-02-18 19:02:15 +08:00
parent ecc031d1ba
commit 81b1d59485
3 changed files with 18 additions and 9 deletions

View File

@ -473,6 +473,10 @@ request.post('/api').form({key:'value'}), function(err,httpResponse,body){ /* ..
更新说明
--------
*v0.0.7 2017年02月18日19:01:48*
1、修改了index.js、config.js详情请看github记录完善了对跨域的处理。
更新建议对应github修改记录同步修改方便将来部署调试。
*v0.0.6 2017年02月17日21:17:23*
1、修改了src/lib/sequelize.js文件,添加了对utf8mb4的支持要求MySQL版本>5.5)。

View File

@ -27,12 +27,14 @@ app
})) // Set Session
.use(PluginLoader(SystemConfig.System_plugin_path))
.use((ctx, next) => {
if (ctx.request.header.host.split(':')[0] === 'api.XXX.com' || ctx.request.header.host.split(':')[0] === '127.0.0.1') {
if (ctx.request.header.host.split(':')[0] === 'localhost' || ctx.request.header.host.split(':')[0] === '127.0.0.1') {
ctx.set('Access-Control-Allow-Origin', '*')
ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
ctx.set('Access-Control-Allow-Credentials', true) // 允许带上 cookie
} else {
ctx.set('Access-Control-Allow-Origin', SystemConfig.HTTP_server_host)
}
ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
ctx.set('Access-Control-Allow-Credentials', true) // 允许带上 cookie
return next()
})
.use(MainRoutes.routes())
@ -49,8 +51,8 @@ if (env === 'development') { // logger
})
}
app.listen(SystemConfig.HTTP_server_port)
app.listen(SystemConfig.API_server_port)
console.log('Now start API server on port ' + SystemConfig.HTTP_server_port + '...')
console.log('Now start API server on port ' + SystemConfig.API_server_port + '...')
export default app

View File

@ -2,9 +2,12 @@ import path from 'path'
// 系统配置
export let System = {
HTTP_server_type: 'http://', // HTTP服务器地址,包含"http://"或"https://"
HTTP_server_host: 'localhost', // HTTP服务器地址,请勿添加"http://"
HTTP_server_port: '3000', // HTTP服务器端口号
API_server_type: 'http://', // API服务器协议类型,包含"http://"或"https://"
API_server_host: 'localhost', // API服务器暴露的域名地址,请勿添加"http://"
API_server_port: '3000', // API服务器监听的端口号
HTTP_server_type: 'http://', // HTTP服务器协议类型,包含"http://"或"https://"
HTTP_server_host: 'www.XXX.com', // HTTP服务器地址,请勿添加"http://" 即前端调用使用的服务器地址如果是APP请设置为 *
HTTP_server_port: '65534', // HTTP服务器端口号
System_country: 'zh-cn', // 所在国家的国家代码
System_plugin_path: path.join(__dirname, './plugins'), // 插件路径
Session_Key: 'RESTfulAPI', // 生产环境务必随机设置一个值