快速表达数据 [英] Express js form data

查看:107
本文介绍了快速表达数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我推荐(最新)的方式来获取POST表单数据。



很多教程/帖子等都谈到bodyParser,但这个不再与Express和其他博客捆绑,建议直接使用urlencoded,但现在这还不可用。



试图找到关于这些框架或技术的准确信息正在做我的头在。



BTW我被介绍的是非常简单和小型的数据

解决方案

您应该通过 npm-install 安装 body-parser 。现在它作为一个单独的中间件。



之后添加以下行在你的app.js

  var bodyParser = require('body-parser'); 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//在最新的body-parser中使用如下。
app.use(bodyParser.urlencoded({extended:true}));

它将 post code>物体。您将在 req.body 中获取变量。



在您的帖子 request handler。

  app.post('/ post',function(request,response){
console.log(request.body)//你将把你的数据作为对象
})


Can someone please tell me the recommended (up to date) way to get POSTed form data in express.

So many tutorials/ posts etc talk about bodyParser but this is no longer bundled with Express and other blogs etc recommend using urlencoded directly, but now this is not available either.

Trying to find accurate information on these frameworks or technologies is doing my head in.

BTW what I am intrerested in is very simple and small form data

解决方案

You should install body-parser through npm-install. Now it comes as a separate middleware.

After that add following line in your app.js

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// in latest body-parser use like below.
app.use(bodyParser.urlencoded({ extended: true }));

It parses the post request as a object. You will get your variables in req.body.

In your post request handler.

app.post('/post',function(request,response){
   console.log(request.body) //you will get your data in this as object.
})

这篇关于快速表达数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆