I've been looking over this basic example in order to set up a simple server in Node, however I am really struggling to understand where the 'request' and 'response' parameters are ing from. Where do they point to and how?
// Load the http module to create an http server.
var http = require('http');
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
}
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(onRequest);
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
Usually when passing paramaters into a function I pass something I can see, like a variable equal to 5, or 'Hello', but in this case I'm not...
Sorry if this is not explained very well!
I've been looking over this basic example in order to set up a simple server in Node, however I am really struggling to understand where the 'request' and 'response' parameters are ing from. Where do they point to and how?
// Load the http module to create an http server.
var http = require('http');
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
}
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(onRequest);
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
Usually when passing paramaters into a function I pass something I can see, like a variable equal to 5, or 'Hello', but in this case I'm not...
Sorry if this is not explained very well!
Share Improve this question edited Dec 12, 2015 at 19:41 Joshua H asked Dec 12, 2015 at 19:39 Joshua HJoshua H 1111 gold badge3 silver badges13 bronze badges 4- They're ing from Node, and they point to the request and response objects created internally by Node. – adeneo Commented Dec 12, 2015 at 19:40
-
And since you're not, you can assume something else is calling them, in this case, the
http
module you require. – Dave Newton Commented Dec 12, 2015 at 19:42 - And if you're extremely interested in figuring out exactly how Node creates those arguments from an ining http call, you can read the source – adeneo Commented Dec 12, 2015 at 19:43
- So when I create the server using the createServer method, that takes the callback function and sets those objects (response, request) to the data that is created upon a request to the server? – Joshua H Commented Dec 12, 2015 at 19:44
2 Answers
Reset to default 6When you call createServer
, you are passing the function onRequest
to it:
var server = http.createServer(onRequest);
This pattern is known as a callback: you pass a function to someone else, with the expectation that they will call your function if something interesting has happened.
In essence, you are saying to Node,
Hey, please create an HTTP server for me. Whenever you receive a request, call my function
onRequest
with the request and response objects passed as parameters.
Another way to do this is to listen to the request
event, which takes the same parameters in its callback.
The parameters are documented as being http.IningMessage
and http.ServerResponse
. You can call them whatever you want, but request
and response
are the idiomatic parameter names. (Some people use req
and res
because they are shorter to type.)
Create server and send response :
1).Create server
var http = require('http');
var server = http.createServer ( function(request,response){
response.writeHead(200,{"Content-Type":"text\plain"});
response.end("Hello");
});
server.listen(8000);
console.log("Server running on port 8000");
2).Save above code and run in cmd .
3).Open browser and go to http://localhost:8000/
Now you see the "Hello"