nodejs express4 获取post方法提交的参数

需要安装bodyparser

npm install body-parser

app.js编写如下

var express=require('express')
//需要body-parser模块
var bodyParser=require('body-parser')

var port=80
var app=express()
//body-parser 解析json格式数据
app.use(bodyParser.json({limit: '1mb'}));
 //此项必须在 bodyParser.json 下面,为参数编码
app.use(bodyParser.urlencoded({
  extended: true
}));
app.listen(port)

app.post('/doLogin',function(req,resp){
  //console.log( req.param("username"));//不赞成
  //使用此方法获取post提交的参数
  var username=req.body.username;
  var password=req.body.password;
  console.log('post==='+username+"---"+password);
  resp.send('获取参数成功!');
})

感谢以下文章
http://yijiebuyi.com/blog/38f1437bf5b43fcf90e6529a81f258f1.html


原文出处:/show_1C7bcPZ6iG9.html