相思资源网 Design By www.200059.com

本文实例为大家分享了基于Express框架使用POST传递Form数据的具体代码,供大家参考,具体内容如下

客户端使用Form发送数据

在客户端Html文件中Form代码如下:

<!-- POST test -->
<form action="/test" method="post" id="foo" >
 <input type="text" name="username">
 <input type="password" name="password">
 <input type="submit">
</form>

在服务器端处理'/test' POST请求的代码如下: 

var bodyParser = require('body-parser');
 
// ... 
 
// create application/json parser
var jsonParser = bodyParser.json()
 
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
 
// ... 
 
// 
// request from form of the html file
// 
app.post('/test', urlencodedParser, function(req, res) { 
 if (!req.body) return res.sendStatus(400);
 
 console.log('Username: ' + req.body.username);
 console.log('Password: ' + req.body.password);
 
 res.send('Welcome, ' + req.body.username); 
});

客户端使用Node.js发送数据

在客户端使用Node.js发送Form数据的代码

const http = require('http');
 
var data = { 
 username: 'foo', 
 password: "test" 
}; 
 
var postData = require('querystring').stringify(data); 
console.log( postData ); 
 
function form()
{
 var options = { 
 method: "POST", 
 host: "localhost", 
 port: 80, 
 path: "/test", 
 headers: { 
  "Content-Type": 'application/x-www-form-urlencoded', 
  "Content-Length": postData.length 
 } 
 }; 
 
 var body = ''; 
 var request = http.request( options, function(res) { 
 // show results 
 console.log('STATUS: ' + res.statusCode); 
 res.setEncoding('utf8'); 
 res.on('data', function(chunk) { 
  body += chunk;
  console.log('BODY: ' + chunk); 
 }); 
 
 res.on('end', function(err) { 
  console.log( ' complete.'); 
 }); 
 }); 
 request.on("error", function(e) { 
  console.log('upload Error: ' + e.message); 
 }) 
 
 request.write( postData );
 request.end(); 
}
 
form();

客户端使用jQuery发送数据

客户端使用jQuery的 $.ajax post数据,

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Post Data</title>
 <script src="/UploadFiles/2021-04-02/jquery.min.js">

client.js 

$(document).ready(function(){ 
 
 //try joining the server when the user clicks the connect button
 $("#startButton").click(function () {
 var filename = $("#input[name=filename]").val();
 
 $.ajax({
 type: 'POST',
 url: "/update",
 // dataType: "jsonp",
 data: { "filename": filename} , 
 jsonpCallback: 'callback', 
 success: function (data) {
  // ...
 },
 error: function (xhr, status, error) {
  console.log('Error: ' + error.message);
 },
 });
 
 });
});

server端使用node.js解析数据

//
// Modules 
var express = require('express'); 
var bodyParser = require('body-parser'); 
 
//
// Global variables 
var app = express(); 
 
// body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
 
 
/* POST /update listing. */
app.post('/update', function(req, res, next) {
 // Checking require
 if (!req.body) return res.sendStatus(400); 
 
 console.log('filename: ' + req.body.filename); 
 
 res.redirect('./');
});

参考文献:expressjs/body-parser 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Express,POST,Form

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“基于Express框架使用POST传递Form数据”

暂无基于Express框架使用POST传递Form数据的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。