senchalabs / connect

Connect is a middleware layer for Node.js
MIT License
9.84k stars 1.09k forks source link

a coding problem in the file of index.js? #1089

Closed gongwg closed 7 years ago

gongwg commented 7 years ago

In the file index.js, there may be a coding problem in the function of next(). In this function, I find there is a segment of code below: if (route.length !== 0 && route !== '/') { removed = route; req.url = protohost + req.url.substr(protohost.length + removed.length);

  if (!protohost && req.url[0] !== '/') {
    req.url = '/' + req.url;
    slashAdded = true;
  }
}

It first references the "length" property of "protohost" object, which indicates that "protohost" must not be NULL. However, in the next line, the if statement's condition contains "!protohost", which checks wether the "protohost" is NULL or not, indicating that the "protohost" may be NULL. This is a contradiction. Perhaps a coding problem here?

dougwilson commented 7 years ago

Hi @gongwg ! In JavaScript, the statement !variable does a lot all at once, not just a null check. In particular, !variable includes the check variable.length == 0, which is what is happening here: it checks that protohost is an empty string.

gongwg commented 7 years ago

Oh, aha, I get it. Thanks for your apply, @dougwilson :)