0 Star 0 Fork 0

newface/CandyUI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.js 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
百灵鸟科技 提交于 2015-08-04 14:58 . No commit message
/**
* 使用nodejs来实现本地开发环境的跨域请求转发,发布为手机应用后通过phonegap的白名单机制来实现跨域
* User: walker
* Date: 12-8-29
* Time: 上午9:44
*/
var http = require('http');
var path = require('path');
var urlparse = require('url').parse;
var express = require('express');
var app = express();
app.use("/proxy", function (req, res) {
var url = req.url.substr(5);
var target = urlparse(url);
var headers = {};
for (var k in req.headers) {
if (k === 'host' || k === 'connection') {
continue;
}
headers[k] = req.headers[k];
}
console.log(url);
console.log(target.path);
var options = {
host: target.hostname,
port: target.port || 80,
path: target.path,
method: req.method,
headers: headers
};
var proxyReq = http.request(options, function (response) {
res.writeHead(response.statusCode, response.headers);
response.on('data', function (chunk) {
res.write(chunk);
});
response.on('end', function () {
res.end();
});
});
proxyReq.on('error', function (err) {
proxyReq.abort();
res.writeHead(500);
res.end(url + ' error: ' + err.message);
});
req.on('data', function (chunk) {
proxyReq.write(chunk);
});
req.on('end', function () {
proxyReq.end();
});
});
app.use(express.static(__dirname + '/demo'));
app.listen(3000);
console.log("Server is launching at http://localhost:3000");
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/scalliance/CandyUI.git
git@gitee.com:scalliance/CandyUI.git
scalliance
CandyUI
CandyUI
master

搜索帮助