http node
http node
server.listen(3000, () => {
console.log('Server in ascolto su http://localhost:3000');
});
🔍 Spiegazione
http.createServer(callback): crea un server. Il callback riceve due oggetti:
res.end('ok');
});
Per leggere il body di una richiesta POST, devi farlo manualmente (a meno di usare
librerie):
ts
Copia
Modifica
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log('Body ricevuto:', body);
res.end('Ricevuto');
});
🧠 Quando usare http direttamente?
Per capire come funziona il basso livello di Node.
Ma per progetti reali, meglio usare Express, che semplifica tantissimo gestione di
routing, body, query, errori, ecc.
Se vuoi ti posso fare un confronto tra http e Express, o farti vedere come tradurre
un server http in uno Express. Vuoi?