2

In node JS Express, we can write middlewares to intercept requests to either

  1. Call the next middleware in the chain by invoking next
  2. End the chain by calling res.send or similar functions provided by res

That means, everytime we want to end a request and send a response in a particular middleware, we have to add (at least) the below snippet.

res.send();

Are there ways to write a response frame middleware like this:

responseFrame = (res,req,responseData) => {
    res.send(responseData);
}

and insinde route.js, use this middleware on all path

app.use(responseFrame);

Then, we simply have to end any middleware with next(), as long as we define the correct routes, Express will take care of sending the response (if the next middleware is the responseFrame)

Tran Triet
  • 1,257
  • 2
  • 16
  • 34
  • If I understand it correctly, you want to transmit data between your middlewares? If you want that, you can simply set local variables inside the req object. The third argument is always filled by express with the next middleware in the stack. – Lasoloz May 07 '18 at 10:10
  • You want to write a middleware that will be responsible for sending response for every route automatically? Correct me if I'm wrong. – Farhan Tahir May 07 '18 at 10:24
  • @Lasoloz yes I guess to achieve what I want, I will need to transmit data between middlewares, but if there are other ways (other than writing a seperate middleware) than I'm all ears. However I don't like to have to modify the req object, it looks somehow counterintuitive to me. – Tran Triet May 07 '18 at 10:44
  • @Farhan I want to write a wrapper for all possible responses. I still handle different routes with differently, but the response frame is always the same. I don't want to have to write that frame everytime I response something. – Tran Triet May 07 '18 at 10:44
  • 4
    I achieved this via writing a helper method called `sendResponse` and instead of using `res.send()` I use `sendResponse(res, data)` – Farhan Tahir May 07 '18 at 10:45
  • Thank you, your suggest certainly reduces boiler plate code. Even though it is not what I was looking for (I was looking for a way so that you don't have to call any other functions explicitly other than next()), it helps. – Tran Triet May 07 '18 at 11:11

1 Answers1

1

You can use res.locals for that.

https://expressjs.com/en/api.html#res.locals

Ishmeet Singh
  • 1,218
  • 1
  • 8
  • 10
  • If I need to pass a parameter from a previous middleware, are there other ways than modifying the req object? – Tran Triet May 07 '18 at 10:47
  • 2
    Well, here is your answer - https://stackoverflow.com/questions/18875292/passing-variables-to-the-next-middleware-using-next-in-expressjs – Ishmeet Singh May 07 '18 at 10:52
  • 1
    Thank you, the link about `res.locals` is nice! I did not know about it. I think I can use this to write a responseFrame middleware, passing in data without having to modify the req object. It sounds logic to me because the express doc says `An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle` It sounds like that's the place I should additional information from previous middlewares. If you could kindly repost this as an answer, I would gladly accept it. – Tran Triet May 07 '18 at 11:14