Atitit 接受post raw数据目录1. 接受get参数 11.1. //req.query只能拿到get参数 //post请求使用 body-parser拿到 12. 原生expre

这篇博客介绍了如何在Express应用中使用body-parser中间件来处理GET和POST请求。特别是,它详细说明了body-parser如何让开发者能够获取POST请求的纯文本、JSON数据和URL编码的数据,并提供了示例代码来展示如何设置和使用body-parser。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Atitit 接受post raw数据

目录

1. 接受get参数

1.1. //req.query只能拿到get参数 //post请求使用 body-parser拿到

2. 原生express并没有处理post的方法,所以需要用到中间件body-parser

2.1. //req.query只能拿到get参数 //post请求使用 body-parser拿到

2.2. 接受post的纯文本与json数据  uelendcode数据

  1. 接受get参数
    1. //req.query只能拿到get参数 //post请求使用 body-parser拿到

  1. 原生express并没有处理post的方法,所以需要用到中间件body-parser

    1. //req.query只能拿到get参数 //post请求使用 body-parser拿到
    2. 接受post的纯文本与json数据  uelendcode数据

corecode


//paresr  can use mlt in same time,auto invoke where client is send..thend set in req.body..
// parse application/json
 app.use(bodyParser.json());
app.use(bodyParser.raw());

// auto invoke by parse where client is   'Content-Type: text/plain; charset=utf-8',
app.use(bodyParser.text());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
app.post("/pst",function(req,res){
    console.log(JSON.stringify(req.body));
    res.end( "ok.." );
})



app.post("/pstxt",function(req,res){
    console.log( (req.body));
    res.end( "pstxt ok.." );
})

Fullcode


// 安装依npm install body-parser express --save-dev
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');//解析,req.bodypost参数
app.get('/a1', function (req, res) {
    res.end( "halo" );
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.post("/pst",function(req,res){
    console.log(JSON.stringify(req.body));
    res.end( "ok.." );
})

var server = app.listen(888, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("Example app listening at http://%s:%s", host, port)
});

//  npm install express   ...open ide termnal view
//http://localhost:888/a1
//http://localhost:888/pst

Use body-parser Express Middleware to Parse Text and URL-Encoded Requests | by John Au-Yeung | Level Up Coding

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值