Node.js - 如何从html发送数据来表达 [英] Node.js - How to send data from html to express
问题描述
这是HTML格式的示例:
<!DOCTYPE html>
< html>
< head>
< meta charset =utf-8/>
< title> CSS3联络表格< /标题>
< / head>
< body>
< div id =contact>
< h1>发送电子邮件< / h1>
< form action =/ myactionmethod =post>
< fieldset>
< label for =name>名称:< / label>
< input type =textid =namename =nameplaceholder =输入您的全名/>
< label for =email>电子邮件:< / label>
< input type =emailid =emailplaceholder =输入您的电子邮件地址/>
< label for =message>消息:< / label>
< textarea id =messageplaceholder =你在想什么?>< / textarea>
< input type =submitvalue =发送消息/>
< / 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('运行于http://127.0.0.1:8080/'的服务器);
我有两个问题:
- 如何从html页面调用node.js中的
myaction
函数?因为html文件在端口80上运行,而node.js在8080上运行(当我尝试将node.js移动到端口80时,它会写入// Unhandled错误'event') - 在node.js函数中,我把?????我怎样才能从HTML表单中获取数据。
当我输入req.html.body.name时,我没有收到数据...
使用 http.createServer
是非常低级的,对于创建web应用程序是没有用的。
在它之上使用的一个好的框架是Express,我会认真地建议使用它。您可以使用 npm install express
来安装它。
当您有时,您可以创建一个基本应用程序来处理您的形式:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//请注意,在Express版本4中,express.bodyParser()是
//弃用赞成单独的body-parser模块。
app.use(bodyParser.urlencoded({extended:true}));
//app.use(express.bodyParser());
app.post('/ myaction',function(req,res){
res.send('你发送了名字''+ req.body.name +'。' );
});
app.listen(8080,function(){
console.log('运行于http://127.0.0.1:8080/'的服务器);
});
您可以使用以下命令将表单指向它:
< form action =http://127.0.0.1:8080/myactionmethod =post>
无法在端口80上运行节点的原因是因为该端口上已经有一个进程正在运行(它服务于你的 index.html
)。您可以使用Express来使用静态内容,如 index.html
,使用 express.static
中间件。
this is form example in 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>
and this is node.js function that run on the 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 have 2 questions:
- How can I call
myaction
function in the node.js from the html page? Because the html file runs on port 80 and node.js on 8080 (when I try to move the node.js to port 80 it writes "// Unhandled 'error' event") - In the node.js function where I put "?????" how can I get data from the html form. When I type req.html.body.name I don't get the data...
Using http.createServer
is very low-level and really not useful for creating web applications as-is.
A good framework to use on top of it is Express, and I would seriously suggest using it. You can install it using npm install express
.
When you have, you can create a basic application to handle your form:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//Note that in version 4 of express, express.bodyParser() was
//deprecated in favor of a separate 'body-parser' module.
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(express.bodyParser());
app.post('/myaction', function(req, res) {
res.send('You sent the name "' + req.body.name + '".');
});
app.listen(8080, function() {
console.log('Server running at http://127.0.0.1:8080/');
});
You can make your form point to it using:
<form action="http://127.0.0.1:8080/myaction" method="post">
The reason you can't run Node on port 80 is because there's already a process running on that port (which is serving your index.html
). You could use Express to also serve static content, like index.html
, using the express.static
middleware.
这篇关于Node.js - 如何从html发送数据来表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!