Skip to content

Commit

Permalink
- 修复开启代理服务器数据类型bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Baiang committed May 11, 2016
1 parent a5e4a09 commit 695861a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
24 changes: 24 additions & 0 deletions lib/dateFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}

for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}


module.exports = format;
45 changes: 27 additions & 18 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
* Created by Administrator on 2016/5/6.
*/

var urlparse = require('url'),
var http = require('http'),
urlparse = require('url'),
superagent = require('superagent'),
dateFormat = require('../lib/dateFormat'),
getIPAdress = require('../lib/ipAdress'),
express = require('express'),
app = express();

var url = 'http://www.dafangya.com.cn';
//app.use(require('body-parser')());
Date.prototype.format = dateFormat;

var bodyParser= require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

function server(port,domain) {
var headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "X-Requested-With",
"Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS",
"X-Powered-By": ' 3.2.1',
"Content-Type": "application/json;charset=utf-8"
};
var url = domain || 'http://www.dafangya.com.cn'; //cli 开启指定域名
var port = port || 8010;
var ip = 'http://' + getIPAdress() + ':' + port,
localIp = 'http://127.0.0.1:'+ port;
app.all('*', function(req, res, next) {
//console.log(req);
//res.writeHead(200,headers);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
// console.log(req.body);
if(req.url == '/favicon.ico'){
return;
Expand All @@ -39,8 +37,10 @@ function server(port,domain) {

if(/^(\w+:\/\/)?\w+(\.\w+)+.*$/.test(pathurl)){
url = pathurl; //ajax指定开启域名
}else{
url = domain || 'http://www.dafangya.com.cn'
}

//post
superagent[req.method.toLocaleLowerCase()](url + req.url)
.send(req.body)
Expand All @@ -55,24 +55,33 @@ function server(port,domain) {
}
console.log(('\n [Request URL] ' + infourl).gray);
console.log((' [Request Method] ' + req.method).gray);
//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
if (err) {
//console.log(err);
if(data){
console.log((' [Error Code] ' + data.status + ' Method Not Allowed').red);
res.writeHead(err.status, headers);
console.log((' [Error Code] '+ data.status + ' ' + http.STATUS_CODES[data.status]).red);
res.sendStatus(err.status);
}else{
console.log((' [Error Status] '+ url + ' 请求超时').red);
}
//console.timeEnd(' [Time]'.gray);
console.log((' ['+ new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ' UTC]')).gray);
console.log((' [' + nowStr + ']').gray);
res.end(req.url);
return;
}
console.log((' [Scuess Code] ' + data.status + ' OK').yellow);
console.log((' ['+ new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ' UTC]')).gray);
res.writeHead(data.status, headers);
console.log((' [Scuess Code] '+ data.status + ' ' + http.STATUS_CODES[data.status]).yellow);
console.log((' [' + nowStr + ']').gray);
// res.sendStatus(data.status);
if (data.ok) {
res.end(data.text);
if(typeof data.text == 'object'){
res.header('Content-Type', 'application/json;charset=utf-8');
res.json(data.text);
}else{
res.header('Content-Type', 'text/plain');
res.end(data.text);//data.text
}
}
});
});
Expand Down

0 comments on commit 695861a

Please sign in to comment.