开发者

Nodejs output -Domain name not found

Technically this is my first try in nodejs and frankly I am not sure if I am doing it right. I am creating a local server that will stream the output from a distant server. However, when I run my code and I enter a URL in the browser, the program fails with the following message:

events.js:45
    throw arguments[1]; // Unhandled 'error' event
                   ^
Error: ENOTFOUND, Domain name not found
at IOWatcher.callback (dns.js:74:15)

The URL I used was: 127.0.0.1:9000/http://www.yahoo.fr. And in the browser I had the following message:

No data received Unable to load the webpage because the server sent no data. Here are some suggestions: Reload this web page later. Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

Any help is greatly appreciated. Here is the code:

var base, dest, node_client,
count = 0,
url = require('url')开发者_如何学JAVA,
util = require('util'),
http = require('http'),
http_client = require('http'),
request = require('request'),
events = require('events'),  
httpProxy = require('./lib/node-http-proxy'),

data_emitter = new events.EventEmitter();

httpProxy.createServer(9000, 'localhost').listen(8000);

http.createServer(function (req, res) {

    if(!count)
    { 
        base = url.parse(req.url).pathname;
        node_client = http_client.createClient(80, base);
        count++;
    } else {
        dest = req.url.substr(1, req.url.length -1);
    }

    request = node_client.request("GET", dest, {"host": base});
    request.addListener("response", function (response) {
        var body = "";
        response.addListener("data", function (data) {
            body +=data;
        }); 

        response.addListener("end", function () {
            var out = JSON.parse(body);
            if(out.length > 0) {
                data_emitter.emit("out", out);
            }
        });
    });

   // request.close();

    var listener = data_emitter.addListener("data", function(out) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(JSON.stringify(out));
        res.close();
    });



}).listen(9000);


Wild guess : your browser automatically requests 127.0.0.1:9000/favicon.ico and your program then tries to resolve favicon.ico which obviously fails and makes your program crash before it can send any data for the real request.


Why such tangled code?

This is a scenario where it makes sense to avoid nested callbacks, and use named functions. If you refactor the code, then people are more likely to be help you.


Can you do console.log(out) in your listener callback? Let us know if Node.js has any response data to return.


Well, for any newbie like me in this area, here is how I solved it. It's not clean and can be implemented in better way. Feel free to change, give suggestions. Code:

var url = require('url'),
http = require('http'),
request = require('request'),
httpProxy = require('./lib/node-http-proxy'),
des = '',

util = require('util'),
colors = require('colors'),

is_host = true;

httpProxy.createServer(9000, 'localhost').listen(8000);

var server = http.createServer(function (req, res) {
var pathname = '';

if(is_host) {
    dest = req.url.substr(0, req.url.length -1);
    pathname = dest;
    is_host = false;
} else {
    pathname = req.url.substr(0, req.url.length);
    if(pathname.charAt(0) == "/") {
        console.log('new request');
        console.log(pathname);
        pathname = dest + pathname;
    }
}

console.log(pathname);
request.get({uri: pathname}, function (err, response, html) {
        res.end(html);
});
console.log('fetched from ' + pathname);
});

server.listen(9000);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜