応答するコールバック関数内の結果を書きたいのですが、関数内の res 変数にアクセスできず、関数外の結果にもアクセスできません。
では、内部と外部の間で値を渡すにはどうすればよいでしょうか。
var http = require('http');
var url = require('url');
http.createServer
(
function (req, res)
{
var output='';
res.writeHead(200, {'Content-Type': 'text/plain'});
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
//output+=str; //get result
});
}
http.request(options, callback).end();
//output+=str; //get result
res.end(output);
}
).listen(80, '127.0.0.1');
ver2
var http = require('http');
var url = require('url');
http.createServer
(
function (req, res)
{
var output='';
res.writeHead(200, {'Content-Type': 'text/plain'});
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
res.write('inside');
});
}
http.request(options, callback).end();
res.write('outside');
res.end(output);
}
).listen(80, '127.0.0.1');
ver3
var http = require('http');
var url = require('url');
http.createServer
(
function (req, res)
{
res.writeHead(200, {'Content-Type': 'text/plain'});
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(res) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
res.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
res.on('end', function () {
res.write('inside');
//or
this.write('inside');
});
}
http.request(options, callback).end();
res.write('outside');
res.end();
}
).listen(80, '127.0.0.1');
ver4
var http = require('http');
var url = require('url');
http.createServer
(
function (req, res)
{
res.writeHead(200, {'Content-Type': 'text/plain'});
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
res.write('inside');
});
}
http.request(options, callback);
res.write('outside');
res.end();
}
).listen(80, '127.0.0.1');