0

I developed a nodejs api to listen to http requests then add some header values and forward it to another Restful service in different server. When the response returns, I will forward same response to requester without doing any modification. so basically this api will work as a forward proxy.

I used below code to forward the response to requester, It returns data however one of our response includes an attachment(pdf file) which will always receive to requester as an empty file/corrupted. Can anybody please help me to resolve this?

app.use('/', function(req, res, next){

    var request = require('request');

    var url={{someurl}} 

    var option = {
        method:req.method,
        json:data,
        headers:{
           "Content-Type": "application/json",
           "Cookie":result
        };
    };
    request(URL, option, function (error, response, body) {   

        if (error) {
            res.status("500").send(error);
            res.end(err);
        }else{            
            if (req.method=='GET'){
                res.set(response.headers);
            }

            res.status(response.statusCode).send(body);
            res.end(response);
        }
    })
  });
Lilan Sameera
  • 39
  • 1
  • 11

2 Answers2

0

for express

const express = require('express');
let router = express.Router();
router.get('/{{someurl}}', (req,res)=>{
  // do your work
  res.send({{any response}});
});
Basit
  • 862
  • 1
  • 12
  • 30
  • the purpose of this service is to listen to http request and forward it to different URL after adding few headers. so basically I cant have routing in my service. – Lilan Sameera Feb 21 '19 at 01:40
0

I found the solution for this issue. in option I have to add encoding: null, otherwise body will be converted to string. when the body converted to string, file will be corrupted.

Lilan Sameera
  • 39
  • 1
  • 11