var http = require('http'); var fs = require('fs'); http.createServer(function(req, res) { // res.writeHead(200, {'Content-Type':'text/plain'}); fs.readFile( './index.html', function(err, data){ if ( err ){ res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('Erreur 404: Fichier non trouvé\n'); } else { res.writeHead(200, {'Content-Type': 'text/html' }); // retourne le fichier trouvé res.end( data ); } }); }).listen(8080); console.log("listening on 8080 at localhost");
Passage de paramètres:
var http = require('http'); var url = require('url'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type':'text/plain'}); var url_parts = url.parse(req.url,true); res.end('Hello World!\n' + url_parts.query['nom']); }).listen(8080); console.log("listening on 8080 at localhost");