body-parser deprecated undefined extended: provide extended option ajax.js:11:20

 下面这个是我转载别人的,只是为了方便我自己学习。

转载原网址:http://t.csdn.cn/yUrjx

bodyParser.json(options) options可选 , 这个方法返回一个仅仅用来解析json格式的中间件。这个中间件能接受任何body中任何Unicode编码的字符。支持自动的解析gzip和 zlib。

bodyParser.urlencoded(options) options可选,这个方法也返回一个中间件,这个中间件用来解析body中的urlencoded字符, 只支持utf-8的编码的字符。同样也支持自动的解析gzip和 zlib。

链接:https://www.zhihu.com/question/36962099/answer/125337339

bodyParser.json是用来解析json数据格式的。bodyParser.urlencoded则是用来解析我们通常的form表单提交的数据,也就是请求头中包含这样的信息: Content-Type: application/x-www-form-urlencoded

常见的四种Content-Type类型:

application/x-www-form-urlencoded 常见的form提交

multipart/form-data 文件提交

application/json 提交json格式的数据

text/xml 提交xml格式的数据

bodyParser.urlencoded 模块用于解析req.body的数据,解析成功后覆盖原来的req.body,如果解析失败则为 {}。该模块有一个属性extended,官方介绍如下:

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). Defaults to true, but using the default has been deprecated.

大致的意思就是:extended选项允许配置使用querystring(false)或qs(true)来解析数据,默认值是true,但这已经是不被赞成的了。
原文链接:https://blog.csdn.net/weixin_42321619/article/details/108094370

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以参考下面的示例代码: ```javascript const express = require('express'); const mysql = require('mysql'); const bodyParser = require('body-parser'); const app = express(); const PORT = 3000; // 创建 MySQL 连接池 const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'mydb', connectionLimit: 10 // 连接池大小,默认为 10 }); // 使用 body-parser 中间件处理 POST 请求参数 app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // 处理 GET 请求 /search app.get('/search', (req, res) => { const { entity, attribute, keyword } = req.query; const sql = `SELECT * FROM ${entity} WHERE ${attribute} = ?`; pool.getConnection((err, connection) => { if (err) throw err; connection.query(sql, [keyword], (error, results) => { connection.release(); if (error) throw error; res.json(results); // 返回 JSON 格式的查询结果 }); }); }); // 监听 3000 端口 app.listen(PORT, () => { console.log(`Server is running on port ${PORT}.`); }); ``` 在这段代码中,我们首先引入了 Express、mysql 和 body-parser 模块,并创建了 Express 应用程序实例。然后创建了一个 MySQL 连接池,并设置了一些配置参数。接着使用 body-parser 中间件处理 POST 请求参数。最后处理了 GET 请求 /search,从 URL 参数中获取实体、属性和关键字,并构造 SQL 语句进行查询。查询结果会以 JSON 格式返回给客户端。最后监听 3000 端口,启动服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值