天天看點

node伺服器接口不穩定,Node做中轉伺服器轉發接口

由于項目在做前後端分離,牽扯跨域和誇協定問題,臨時抱佛腳,選擇用nodejs做中轉,我想應該好多人都用它。但是做普通的表單轉發沒啥問題,當處理附件上傳轉發時,各種蛋疼,已解決!

1.項目比較特殊,背景擁有兩個平台,一個java一個donet,比較雞肋,具體什麼原因就不解釋了。

2.當做node轉發時,剛開始沒有轉發檔案的操作,就做的很簡單,使用者傳過來啥就,攔截到,進行轉發,一切都很ok!

3.檔案轉發,就很麻煩。我的思路,将使用者上傳的檔案存到node伺服器。使用formidable 。

通過npm安裝:

使用它進行檔案轉存,儲存到臨時目錄得到檔案資訊。

再通過檔案包重組。進行上傳。注意此處上傳必須遵循w3c上傳檔案表單标準,具體自己查資料。

其實思路很簡單,但是實際操作起來還是挺麻煩,我中間也趟了好多坑,也是自己node不成熟,畢竟隻是用來做中轉!

直接上代碼吧:看代碼還是清晰:

server.js,用于啟動服務并轉發。

>>>>" + prams);

const postData = prams;

console.log("client pramsJson>>>>>" + postData);

var contenttype = request.headers['content-type'];

if (contenttype == undefined || contenttype == null || contenttype == '') {

var opt = {

host: javaServerhost,//跨域通路的主機ip

port: javaServerport,path: "/hrrp" + clientUrl2,headers: {

'Content-Length': Buffer.byteLength(postData)

}

}

} else {

var opt = {

host: javaServerhost,headers: {

'Content-Type': request.headers['content-type'],'Content-Length': Buffer.byteLength(postData)

}

}

}

var body = '';

console.log('method',opt.method);

var req = http.request(opt,function (res) {

//console.log("response: " + res.statusCode);

res.on('data',function (data) {

body += data;

}).on('end',function () {

response.write(body);

response.end();

//console.log("end:>>>>>>>" + body);

});

}).on('error',function (e) {

response.end('内部錯誤,請聯系管理者!MSG:' + e);

console.log("error: " + e.message);

})

req.write(postData);

req.end();

})

} else if (pathname.match(fileServerUrlFlag) != null) {

//檔案攔截儲存到本地

var form = new formidable.IncomingForm(),files = [],fields = [];

form.uploadDir = os.tmpdir();

form.on('field',function (field,value) {

console.log(field,value);

fields.push([field,value]);

}).on('file',file) {

console.log(field,file);

files.push([field,file]);

}).on('end',function () {

//

var clientUrl2 = clientUrl.replace("/" + fileServerUrlFlag,'');

var opt = {

host: netServerhost,method: request.method

}

var body = '';

var req = http.request(opt,function (res) {

res.on('data',function () {

response.write(body);

response.end();

});

}).on('error',function (e) {

response.end('内部錯誤,請聯系管理者!MSG:' + e);

console.log("error: " + e.message);

})

//檔案上傳

uploadFile(files,fields);

});

form.parse(sreq);

}

else {

var realPath = path.join(webapp,pathname); //這裡設定自己的檔案名稱;

var ext = path.extname(realPath);

ext = ext ? ext.slice(1) : 'unknown';

fs.exists(realPath,function (exists) {

//console.log("file is exists:"+exists+" file path: " + realPath + "");

if (!exists) {

response.writeHead(404,{

'Content-Type': 'text/plain'

});

response.write("This request URL " + pathname + " was not found on this server.");

response.end();

} else {

fs.readFile(realPath,"binary",function (err,file) {

if (err) {

response.writeHead(500,{

'Content-Type': 'text/plain'

});

//response.end(err);

response.end("内部錯誤,請聯系管理者");

} else {

var contentType = config[ext] || "text/plain";

response.writeHead(200,{

'Content-Type': contentType

});

response.write(file,"binary");

response.end();

}

});

}

});

}

});

server.listen(PORT);

console.log("Server runing at port: " + PORT + ".");

config.js,用于配置。

dio/x-wav","wma": "audio/x-ms-wma","wmv": "video/x-ms-wmv","xml": "text/xml"

};

exports.netServerUrlFlag = "NETSERVER";

exports.netServerhost = "";

exports.netServerport = "";

exports.javaServerUrlFlag = "JAVASERVER";

exports.javaServerhost = ""; //轉發的位址

exports.javaServerport = "";//轉發的端口

exports.fileServerUrlFlag="FileUpload";

exports.webapp = "public";//項目目錄

exports.webport = "82"; //項目啟動端口

總結

以上所述是小編給大家介紹的Node做中轉伺服器轉發接口。程式設計之家 jb51.cc 收集整理的教程希望能對你有所幫助,如果覺得程式設計之家不錯,可分享給好友!感謝支援。

總結

以上是程式設計之家為你收集整理的Node做中轉伺服器轉發接口全部内容,希望文章能夠幫你解決Node做中轉伺服器轉發接口所遇到的程式開發問題。

如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。

本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。

如您喜歡尋找一群志同道合、互幫互助的學習夥伴,可以點選下方連結加入:

程式設計之家官方1群

程式設計之家官方2群

程式設計之家官方3群

程式設計之家官方4群