questo è esempio modulo in html:Node.js - Come inviare i dati da HTML a esprimere
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS3 Contact Form</title>
</head>
<body>
<div id="contact">
<h1>Send an email</h1>
<form action="/myaction" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" />
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter your email address" />
<label for="message">Message:</label>
<textarea id="message" placeholder="What's on your mind?"></textarea>
<input type="submit" value="Send message" />
</fieldset>
</form>
</div>
</body>
</html>
e questa è la funzione node.js che girano sul server:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
switch (req.url)
case '/myaction':
res.end(?????);
break;
}
}).listen(8080);
sys.puts('Server running at http://127.0.0.1:8080/');
I hai 2 domande:
- Come posso chiamare la funzione
myaction
nel file node.js dalla pagina html? Poiché il file html viene eseguito sulla porta 80 e node.js su 8080 (quando provo a spostare node.js sulla porta 80 scrive "// non gestito" errore "evento") - Nella funzione node.js dove I mettere "?????" come posso ottenere i dati dal modulo html. Quando digito req.html.body.name non ottengo i dati ...
Hai mai pensato di utilizzare Express? Ha un gestore integrato per le forme. Lo uso con grande successo pubblicando moduli. –