node.js - 如何在 node.js 中使用 bodyparser
问题描述
我什么时候在我的代码中使用 bodyparser?
const express = require('express');
const bodyparser = require('body-parser');
const app = express();
app.set('views',__dirname+'/public/pages/');
app.set('view engine','twig');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.get('/',(req, res)=>{
res.render('index',{title:"create user"})
})
app.post('/',(req, res)=>{
console.log(req.body.username);
})
app.listen(8000, ()=>console.log('server is running at:http://localhost:8000'));
当我运行此代码时:错误在此处输入图像描述
解决方案
我什么时候在我的代码中使用 bodyparser ?
该模块的 npm 页面显示,“Node.js 正文解析中间件。在处理程序之前在中间件中解析传入的请求正文,在 req.body 属性下可用。” 在这里阅读更多
推荐阅读
- c++ - 构建解决方案时未构建 Visual Studio 特定项目
- c - 识别小函数的大 O 表示法
- python-3.x - 如何使用 linux 中的链接下载谷歌驱动器文件夹
- java - 包含单例 bean 的原型 bean 在使用后会被垃圾收集吗?
- cakephp - CakePHP 中的用户编辑策略
- flutter - 在颤振中检测已安装的广告拦截器
- maven - 编码器类 (org.owasp.esapi.reference.DefaultEncoder) CTOR 抛出异常
- python - 汽车撞到车窗边缘时会移动并改变方向
- android - 错误:不兼容的类型:int 无法转换为工具栏 R.drawable.ic_drawer & 找不到符号“mDrawerToggle = new ActionBarDrawerToggle(”
- mysql - 无法通过mysql触发器插入表