微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node做中转服务器转发接口

由于项目在做前后端分离,牵扯跨域和夸协议问题,临时抱佛脚,选择用nodejs做中转,我想应该好多人都用它。但是做普通的表单转发没啥问题,当处理附件上传转发时,各种蛋疼,已解决

1.项目比较特殊,后台拥有两个平台,一个java一个donet,比较鸡肋,具体什么原因就不解释了。

2.当做node转发时,刚开始没有转发文件的操作,就做的很简单,用户传过来啥就,拦截到,进行转发,一切都很ok!

3.文件转发,就很麻烦。我的思路,将用户上传文件存到node服务器。使用formidable 。

通过npm安装:

rush:js;"> npm install formidable@latest

  使用它进行文件转存,保存到临时目录得到文件信息。

再通过文件包重组。进行上传。注意此处上传必须遵循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,用于配置。

rush:js;"> exports.types = { "css": "text/css","gif": "image/gif","html": "text/html","htm": "text/html","ico": "image/x-icon","jpeg": "image/jpeg","jpg": "image/jpeg","js": "text/javascript","json": "application/json","pdf": "application/pdf","png": "image/png","svg": "image/svg+xml","swf": "application/x-shockwave-flash","tiff": "image/tiff","txt": "text/plain","wav": "audio/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 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐