Reputation: 125
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})
app.get('/process_get', function (req, res) {
// Prepare output in JSON format
response = {
first_name:req.query.first_name,
last_name:req.query.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
1)I have got the Tutorial from tutorial point
2) I am thinking req in the /process_get will be empty but here the req is have all my data how is possible
3) I am Thinking scope of these function is limited to that function only but how can it access the value of previous function
4) how can we handle if we are having multiple req objects in code
Upvotes: 0
Views: 157
Reputation: 666
Express.js will then call it as part of it's middleware stack when processing a request, giving it the req, res and next arguments.
Upvotes: 0
Reputation: 99
req
is an object that represents the request made to the server. It is an argument to the callback that gets invoked when the server has received the request. You can think of it as the result of app.get to the route /process_get. res
works in the same way but it represents the response sent by the server. res.end()
is what sends back the response payload to the requester.
Both req and res only exist in the scope of the callback function. That’s why there are req and res in callbacks of both route /index.htm
and /process_get
.
Hope this makes it a bit clearer.
Upvotes: 1