mercredi 26 septembre 2012

stream node

   //BUG filestream if no index.html is passed
   //http://localhost:8124/index.html in @ toolbar

var http = require('http'),
    path = require('path'),
    fs = require('fs'),
    _base = '/home/phil/node-book/public';
  

http.createServer( function(req,res) {
    pathname = _base + req.url;
    console.log(pathname);
  
    fs.exists(pathname, function(exists) {
    if (exists)
    {
        console.log("repertoire ok");
        res.setHeader('Content-Type', 'text/html');
        res.statusCode=200; //200 - no errors
        var file=fs.createReadStream(pathname);
        file.on("open", function() {
            file.pipe(res);
            console.log("open ok");
        });
        file.on("error", function(err) {
            console.log(err);
            console.log("error");
        });
       
    }
    else {
        res.writeHead(404);
        res.write('bad request 404 \n');
        res.end();
        }
});
}).listen(8124);

console.log('server running');

Aucun commentaire:

Enregistrer un commentaire