What's the meaning and use of "res" in function arguments?
here's a short question that my googling failed to deliver any clues on.
It seems to be pretty commonplace to use the word "res" for one of the indexes in function arguments. Its existence appears to be agnostic to whatever programming language you look at.
What does it stand for? A simple guess would be, "resource", perhaps? If this is spot on, it would be nice if someone who feels pedagogically inclined, would care to help me shed some light on this. What I don't grok, in particular, is when and why there's a use for the meaning, "resource" (granted that this is the actual meaning of "res").
Edit 1: I'm providing a random ex开发者_开发百科ample, this is from the NodeJS.org homepage:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Edit 2: I first came across the "res" thing while working with a PHP backend developer on a fairly modded WP instance. At the time things were too hectic to really sort out what he meant in his quick explanation, but he seemed to scatter the word all around, in arguments and $res variable calls alike. It's obviously impossible to comment on individual programmers' style and preference of coding, but I'm mostly curious to know if there's some sort of untold consensus among programmers – and if there's a single purpose – using a so called "res" pointer. (Compare eg. with using i
for "iterator" as often used in loops.)
In case it's not quite clear from the various answers and comments, in this context req
is short for request
and res
is short for response
.
A HTTP transaction consists of a request from the client to the server and a response from the server to the client.
Not an answer, but
It seems to be pretty commonplace to use the word "res" for one of the indexes in function arguments. Its existence appears to be agnostic to whatever programming language you look at.
Nope. My guess is this is only common for Javascript doing http request/response (or a few very similar things).
In your code, req
means request and res
response, but res
can also mean "result". A JS example:
function count(haystack, needle) {
var res = 0
arr.forEach(function (value) {
if (value === needle) {
res++
}
})
return res
}
精彩评论