这是HTML表单示例:
<!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>
这是运行在服务器上的Node.js函数:
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/');
我有两个问题:
- 如何在html页面中从node.js调用
myaction
函数?因为html文件运行在80端口,而node.js运行在8080端口(当我尝试将node.js移至80端口时,它会写入“//未处理的'error'事件”) - 在node.js函数中,在哪里放置“?????”,我如何从html表单中获取数据。当我键入req.html.body.name时,我得不到数据...