>
>   var stream = fs.createReadStream(path, options);
>   stream.on('error', function() { /* ... */ });
>

Wouldn't this mean that the error handler is unlikely to be invoked in case
of an error during creation/opening of the stream?

>
> What do you mean with 'fd becomes undefined'? Where/when does that
> happen? A more complete code example would help.

I meant that the values of (err, fd) received by the open callback change
over time. fd is >= 0 and err is null in response to the initial http
requests. After a few seconds the callback parameter values are ('Error:
OK, success 'C:,\files\test.dat', undefined).

var fileName = 'C:\\files\\test.dat';

var errHandler = function (exception) {
    console.log(exception);
};

var reqListener = function (req, res) {
    req.error = errHandler;
    res.error = errHandler;
    var openHandler = function (err, fd) {
        console.log(fd+ ' ' +err);
        if (fd >= 0) {
            options = {};
            options.fd = fd;
            rdstream = fs.createReadStream(null, options);
            rdstream.error = errHandler;

            res.writeHead(200, {
                'Content-Type': 'application/octet-stream'
            });
            rdstream.pipe(res);
        }
    };
    openHandler.res = res;
    fs.open(fileName, 'r', 0666, openHandler);
};

//var hs = http.createServer(reqListener);


The output looks like:

C:\files>"C:\Program Files (x86)\nodejs\node.exe" "C:\Program Files
(x86)\nodejs\hs.js"
3 null
3 null
4 null
4 null
5 null
5 null
6 null
6 null
7 null
7 null
8 null
8 null
9 null
9 null
10 null
10 null
...
...
...
2007 null
1831 null
1872 null
2045 null
1907 null
2008 null
2012 null
1873 null
1832 null
2046 null
1908 null
2009 null
2013 null
1833 null
1874 null
2047 null
1909 null
2010 null
2014 null
1875 null
1834 null
2011 null
1835 null
2015 null
1910 null
2012 null
undefined Error: OK, success 'C:\files\test.dat'
1876 null
2016 null
1911 null
undefined Error: OK, success 'C:\files\test.dat'
1877 null
1912 null
2017 null
1878 null
undefined Error: OK, success 'C:\files\test.dat'
1913 null
1879 null
undefined Error: OK, success 'C:\files\test.dat'
1914 null
1880 null
.... errors increase in frequency

--

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to