0

In a code for a GroupMe chat bot, the variable request is defined as follows:

var request = JSON.parse(this.req.chunks[0]);

I'm trying to figure out what this means. In particlar, what is req referring to?

EDIT: Here is the function where it shows up:

function respond() {
  var request = JSON.parse(this.req.chunks[0]),
      botRegex = /^deadline ([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
      botRegex2 = /^\/oranges$/;

  if(request.text && botRegex.test(request.text)) {
    this.res.writeHead(200);
    postDeadline();
    this.res.end();
  } else if (request.text && botRegex2.test(request.text)) {
    this.res.writeHead(200);
    postOranges();
    this.res.end();
  } else {
    console.log("don't care");
    this.res.writeHead(200);
    this.res.end();
  }
}
George
  • 5,909
  • 2
  • 24
  • 53
  • 1
    It's just a property name; it could be anything. It all depends on what `this` refers to in the context of that statement. – Pointy Dec 24 '14 at 22:53
  • I added the function to which `this` refers to. – George Dec 24 '14 at 23:06
  • That's not enough - it's still the case that it's impossible to know for sure what `this` refers to. It's *probably* an object with properties that refer to an HTTP request (`req`) and response (`res`), but "req" is just a name - the value it refers to *could* be anything. – Pointy Dec 24 '14 at 23:14
  • Why is it impossible to know for sure what `this` refers to in this context? – George Dec 24 '14 at 23:20
  • Because a JavaScript function can be called in such a way as to leave `this` referring to **anything**; that's just the way the language works. We'd have to see how the function is *called* to know for sure. (Again, it's probably something having to do with handling an HTTP request, but it might be part of a library or it might be something written for some special purpose.) – Pointy Dec 24 '14 at 23:26

3 Answers3

4

req is a property on this. So that depends on what this is in this context.

Usually, req is a naming convention kept for objects representing an incoming request.

Madara's Ghost
  • 158,961
  • 49
  • 244
  • 292
  • I added the function to which `this` refers to. Do you know what `req` refers to in this context? – George Dec 24 '14 at 23:06
  • No, because in that function, `this` depends on how the function was called. So it really depends on who's calling it. – Madara's Ghost Dec 24 '14 at 23:26
  • No, because `this` in this context depends on *how the function was called*. Please see this question for more details: http://stackoverflow.com/questions/3127429/javascript-this-keyword – Madara's Ghost Dec 24 '14 at 23:27
2

req is short for request which would be the HTTP request data from the client.

Rob
  • 13,342
  • 26
  • 40
  • 60
1

Typically, the convention is req is part of the request object on an HTTP transaction. If I had to guess form just this small snippet of code, it's a streaming request that's being parsed as JSON. The request is called by the parent object, hence this. Most likely, it's piping JSON data from an HTTP request to an API (perhaps the GroupMe API), and parsing it into an object that can be more easily used by other functions within the code.

EDIT: Unlike this, req isn't a reserved keyword. It's a convention that's commonly used when referring to HTTP transactions, but may not always hold the same context, so to say it definitively refers to a single thing would be foolish without knowing its function.

Brandon Anzaldi
  • 5,648
  • 3
  • 33
  • 51