2013-04-18 7 views
0

test.htmlComment obtenir une page HTML dans nodejs?

<html> 
    <head> 
     <title>Test Page</title> 
    </head> 
    <body> This is the body</body> 
</html> 

Comment puis-je modifier ceci:

var http = require('http'); 
http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:1337/'); 

pour revenir test.html ci-dessus?

+1

Avez-vous essayé la recherche d'abord? Trouvé http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js assez rapidement. –

Répondre

1

Voici un exemple d'un simple serveur statique streaming

var basepath = '/files' 

http.createServer(function (req, res) { 
    if (req.method !== 'GET') { 
    res.writeHead(400); 
    res.end(); 
    return; 
    } 
    var s = fs.createReadStream(path.join(basepath, req.path)); 
    s.on('error', function() { 
    res.writeHead(404); 
    res.end(); 
    }); 
    s.once('fd', function() { 
    res.writeHead(200); 
    }); 
    s.pipe(res); 
}); 

En pratique, vous devez utiliser express.static: http://runnable.com/UWw3g0PKxoAWAA6K

Ou un module statique deticated comme https://github.com/jesusabdullah/node-ecstatic

Questions connexes