0

I'm trying to make my monolithic application written in node and express to a microservice application in node, needle(to communicate between microservices) and express. My architecture is as follows,

  • I have a microservice called gateway_service to handle all the requests from frontend UI
  • I have another microservice called users_service to handle requests related to users, users_service uses passport local strategy to manage sessions and this session id is sent as a cookie by passport

When I try to change my application to microservice I'm doing the following

  • Whenever I receive a request from my client say, postman in gateway_service I can see the cookie coming through header, and I'm able to pass it to user_service by setting headers option as mentioned in needle.
  • How to send the cookie that I receive from user_service to the postman back to he can save the cookie(in case of first hit)??

I'm adding a small code snippet, hoping it will help? Am I doing it in the right way?? when I'm doing as shown below Im getting the error "Cannot set headers after they are sent to the client"

function (req, res, next) {
  var options = {
    headers: req.headers,
    parse_cookies:true
  }
    needle.post(url + "login", req.body, options, function (error, response, body) {
      if (!error) {
        res.writeHead(response.statusCode,response.headers);
        res.send(body)
        console.log(res);
        next();


      } else {
        console.log("error in  info from users micro service");
        res.send(error);
        next();
      }
    });
  },
fad
  • 43
  • 1
  • 9
  • I think you need to provide more info, epecially about things that don't work as expected. What did you expect and what happened? – CoderFF Apr 28 '20 at 17:57
  • I have edited @CoderFF , my intention is to send the exact response structure which users microservice is generating to be passed to the UI client – fad Apr 28 '20 at 18:54
  • I'm sorry, I still can't understand the problem. The description is still vague to me/ – CoderFF Apr 28 '20 at 19:13
  • I' sorry @CoderFF hope this one helps – fad Apr 28 '20 at 20:17
  • if what you are writing is not a middleware, please remove `next` property and all calls to `next()`. you are sending data to the client with `res.send(body)` and then call `next()` that would forward to the next middleware, and get caught somewhere that also sends data back to the client... when writtinga route, unless you know exactly what you are doing, do not use `next` in `function (req, res, next)` – balexandre Apr 28 '20 at 20:26
  • yeah @balexandre is right. also, if your midlleware always sends something as a response then it is pointless to put the code into the middleware cause it doesn't allow controllers/actions do anything. – CoderFF Apr 29 '20 at 11:12

0 Answers0