const url = require('url');
const http = require('http');
const https = require('https'); // cnode是https协议
//导入querystring模块(解析post请求数据)
var querystring = require('querystring');
const server = http.createServer((req, res) => {
//var params = req.url.toString(1, req.url.length);
//var params = url.parse(req.url, true).query;
var data = '';
if (req.method == "GET") {
data = url.parse(req.url, true).query;
data = JSON.stringify(data);
let opt = {
hostname: '',
method: req.method,
path: '',
headers: {
"Content-Type": 'application/json',
"Content-Length": data.length
}
}
if(data){
opt.hostname = JSON.parse(data).baseUrl;
opt.path = JSON.parse(data).pathUrl;
}
let body = '';
let breq = https.request(opt, function (bres) {
bres.on('data', function (data) {
body += data;
}).on('end', function () {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,X-URL-PATH,x-access-token"
})
res.end(body);
});
}).on('error', function (e) {
console.log("error: " + e.message);
})
breq.write(data);
breq.end();
} else {
req.on('data',function(r){
data += r;
})
req.on('end', function () {
var opt = {
hostname: '',
method: req.method,
path: '',
headers: {
"Content-Type": 'application/json',
"Content-Length": data.length
}
}
if(data){
opt.hostname = JSON.parse(data).baseUrl;
opt.path = JSON.parse(data).pathUrl;
}
let body = '';
let breq = https.request(opt, function (bres) {
bres.on('data', function (data) {
body += data;
}).on('end', function () {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,X-URL-PATH,x-access-token"
})
res.end(body);
});
}).on('error', function (e) {
console.log("error: " + e.message);
})
breq.write(data);
breq.end();
})
}
}).listen(3000, '127.0.0.1');
console.log('监听 127.0.0.1:3000,服务已启动');