Collin DeSoto Collin DeSoto - 9 months ago 74
Javascript Question

Changing req object within api server?

I recently picked up a client who has a dev team. I am working on a website that has already been developed and am running into some things that are slightly strange to me.

I've always though it was essentially bad to mess with the request object within route handling (I could be completely wrong here).

The following code reaaaaally confuses me as I am not sure why they are assigning the req.query.msg to something instead of just creating a variable and passing it through on the ejs page render.

/********************************************************
* *
* CHANGE PASSWORD ROUTE THAT POSTS THE NEW PASSWORD TO *
* DATABASE. *
* *
********************************************************/



app.post('/client/password', function (req, res) {
var url = URLS.ClientChangePW;

if(req.session.securityquestions[0].SSN !== "null" || req.session.securityquestions[0].SSN !== "undefined"){
if(req.body.pwd !== req.body.pwdconf){
res.redirect('/client/changePassword' + config.PWD_MISMATCH);
} else {
var ssn = req.session.securityquestions[0].SSN;
while(ssn.length < 9){
ssn = "0" + ssn;
}

url = url.replace("@ssn", ssn);
url = url.replace("@newpw", req.body.pwd);
}

request.put(url, function (err, xres, body) {

var data = JSON.parse(body);


if(data.status === 200){

email(req.session.securityquestions[0].EMAIL, "none", "forgotpw", function(result){

if(result){
req.query.msg = "Your password has been reset.";
} else {
req.query.msg = "Request unsuccessful. Please call number here for assistance.";
}
res.render('pages/login', {
session: req.session,
msg: req.query.msg
});
});
} else {
req.query.msg = "Request unsuccessful. Please call number here for assistance.";
res.render('pages/login', {
session: req.session,
msg: req.query.msg
});
}
});
}
});


Again, I have never really messed with the req object so I could be wrong. I always thought the client sets up the request and we use that to send a response.

Answer Source

I am not sure why they are assigning the req.query.msg to something instead of just creating a variable and passing it through on the ejs page render.

There does not appear to be any reason to be assigning to the req.query.msg property here. If this were my code, I'd be using a separate local variable for it.

Again, I have never really messed with the req object so I could be wrong. I always thought the client sets up the request and we use that to send a response.

Though this is not what is happening here, it is common in Express development to use middleware that sets state on the req object for request handlers further down the routing stack to use. The req object is the canonical object where you keep request-specific state while processing the request. If you only have one request handler function working on the request, then there's no reason to put state on the req object as you can just use local variables in that one request handler function. But, if you're using middleware whose job it is to set things up before ultimately getting to a request handler, then the req object is where that setup state is usually put. You'll notice that req.session is also used in this code. That .session property was put there by some middleware upstream in the request processing.

So, it IS common to add state to the req object when using middleware. But, in the .msg property example in the code you show, there is no particular reason to put that on the req object as its value is only needed in the local function so it can just as easily (and I would argue more clearly) be in a local variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download