Tushar Arora
Tushar Arora

Reputation: 1146

express js - How is one http request different from other?

I've been working on creating a better architecture for rest api in express and node. Let's say I have 3 methods in my route middleware -

router.post('/users/:id', [
  UserService.getUserById,
  UserController.setUser,
  MailerService.sendSubscriptionMail
]);

I am setting up req.session.user in call to UserService.getUserById and then using that to set req.session.result in UserController.setUser. Now I am sending a mail to this user using the data stored in req.session.result.

UserService -

exports.getUserById = function(req, res, next) {
  .
  .
  req.session.user = data;
  .
  .
};

module.exports = exports;

UserController -

exports.setUser = function(req, res, next) {
  .
  .
  req.session.result = req.session.user;
  .
  .
};

module.exports = exports;

MailerService -

exports.sendSubscriptionMail = function(req, res, next) {
  // Using req.session.result here to send email
};

module.exports = exports;

Now I have two questions regarding above process -

(a) Is there any chance that a new http req to another route (which also has these kind of methods which can modify req.session) can modify the req.session.result and MailerService.sendSubscriptionMail does not get the data which it needs to send to the user or will that req object will be completely different from this one in the memory?

(b) Is there any other method to transfer data between middleware rather than setting up req object?

Upvotes: 2

Views: 252

Answers (1)

jfriend00
jfriend00

Reputation: 707326

Is there any chance that a new http req to another route (which also has these kind of methods which can modify req.session) can modify the req.session.result and MailerService.sendSubscriptionMail does not get the data which it needs to send to the user or will that req object will be completely different from this one in the memory?

The req object is specific to this request. That object cannot be changed by another request. But, if the session in req.session is a common shared sesison object that all requests from that particular user share, then req.session.result could be changed by another request from that user that is getting processed at around the same time (e.g. interleaved within various async operations).

If you want to make sure that no other request from this user could change your result, then put it in req.result, not req.session.result because no other requests will have access to req.result.

Is there any other method to transfer data between middleware rather than setting up req object?

The req or res objects are the right places to share info among middleware handlers for the same request as they are unique to this particular request. Be careful with the session object because it is shared among multiple requests from the same user.


Another possible way to share the data among your three handlers is to make a single middleware handler that calls all three of your middleware handlers and then share the data within that single function (e.g. passing it to the other handlers).

For example, you could change the calling signature of your 2nd two methods so you can get the data out of setUser() and then pass it directly to sendSubscriptionMail() without using the req or session objects to store it.

router.post('/users/:id', [UserService.getUserById, function(req, res, next) {
    UserController.setUser(req, res, function(err, result) {
        if (err) return next(err);
        MailerService.sendSubscriptionMail(result, req, res, next);
    }]);
});

Upvotes: 1

Related Questions