nodejs实现表单数据的提交
hello.html
<html> <head> <meta charset="utf-8"> <meta http-equiv="Expires" content="0"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Cache-control" content="no-cache"> <meta http-equiv="Cache" content="no-cache"> <title>菜鸟教程 Node.js 实例 </title> </head> <body> <form method="post" action="http://127.0.0.1:3000/index"> 网站名: <input name="name"><br> 网站 URL: <input name="url"><br> <input type="submit"> </form> </body> </html>
index.js 需要开启node 服务
var http = require('http');
var querystring = require('querystring');
// var postHtml=
// '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
// '<body>' +
// '<form method="post" ">' +
// '网站名: <input name="name"><br>' +
// '网站 URL: <input name="url"><br>' +
// '<input type="submit">' +
// '</form>' +
// '</body></html>';
http.createServer(function (req,res) {
var body = '';
if(req.url == '/index'){
req.on('data',function(chunk){
body +=chunk;
});
req.on('end',function(){
res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
var params = querystring.parse(body);
res.write(params.name);
res.write("\n");
res.write(params.url);
res.end();
});
}
}).listen(3000);
扩展到mysql
var http = require('http');
var querystring = require('querystring');
var mysql =require('mysql');
// var postHtml=
// '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
// '<body>' +
// '<form method="post" ">' +
// '网站名: <input name="name"><br>' +
// '网站 URL: <input name="url"><br>' +
// '<input type="submit">' +
// '</form>' +
// '</body></html>';
http.createServer(function (req,res) {
var body = '';
if(req.url == '/index'){
req.on('data',function(chunk){
body +=chunk;
});
req.on('end',function(){
res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
var params = querystring.parse(body);
connect(params);
// res.write(params.name);
// res.write("\n");
// res.write(params.url);
res.end();
});
}
}).listen(3000);
var pool = mysql.createPool({
host : 'localhost',
port : 3306,
database : 'toys',
user : 'root',
password : ''
});
function connect(params){
pool.getConnection(function(err,connection){
if(err){
console.log('与mysql数据库建立连接失败');
}else{
console.log('与mysql数据库建立连接成功');
connection.query('insert into test set?',{
amount:10,
name: params.name,
url:params.url
},function(err,result){
if(err){
console.log('插入数据失败');
}else{
console.log('插入数据成功');
//当一个连接不需要使用时,将其归还到连接池中
connection.release();
//关闭连接池
//pool.end();
}
});
}
})
//处理数据库服务器连接中断时的操作
pool.on('error',function(err){
if(err.code === 'PROTOCOL_CONNECTION_LOST'){
console.log('与mysql数据库之间的连接丢失');
//3秒后重新尝试连接数据库
setTimeout(function(){
connect();
},3000);
}else{
throw err;
}
})
}
注意:由于跨域问题,使用CORS解决方案
JSONP由于传输数据量较大,需要使用POST方式提交,所以使用CORS技术(跨域资源共享)
CORS定义一种跨域访问的机制,允许一个域上的网络应用向另一个域提交跨域AJAX请求,只需由服务器发送一个响应标头即可
例子:(PHP环境)
header("Access-Control-Allow-Origin:*"); //*表示允许任何域向我们的服务端提交请求
header("Access-Control-Allow-Origin:http://www.test.com"); //只允许指定的域
(NodeJS环境)
res.setHeader("Access-Control-Allow-Origin","*");
response.writeHead(200,{"Content-Type":'text/plain','charset':'utf-8','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});//可以解决跨域的请求
执行时,可能提示cannot find moudle 'mysql',请在项目目录下安装mysql模块即可:npm install mysql

浙公网安备 33010602011771号