这是我的简单表单:

<form id="loginformA" action="userlogin" method="post">
    <div>
        <label for="email">Email: </label>
        <input type="text" id="email" name="email"></input>
    </div>
<input type="submit" value="Submit"></input>
</form>

这是我的Express.js/Node.js代码:

app.post('/userlogin', function(sReq, sRes){    
    var email = sReq.query.email.;   
}

我试过sReq。query。email或sReq。查询['email']或sReq。params['邮件'],等等。没有一个有用。它们都返回undefined。

当我更改为Get调用时,它可以工作,所以。任何想法?


当前回答

你不应该使用app.use(express.bodyParser())。BodyParser是json + urlencoded + multipart的组合。你不应该这样做,因为multipart将在connect 3.0中被删除。

要解决这个问题,你可以这样做:

app.use(express.json());
app.use(express.urlencoded());

知道app.use(app.router)应该在json和urlencoded之后使用是非常重要的,否则它就不起作用!

其他回答

对于POST和GET请求,我可以使用以下代码找到所有参数。

var express = require('express');
var app = express();
const util = require('util');
app.post('/', function (req, res) {
    console.log("Got a POST request for the homepage");
    res.send(util.inspect(req.query,false,null));
})

在Express 4.16版编写

在路由器函数中,你可以使用req。属性来访问post变量。例如,如果这是你的表单的POST路由,它会返回你输入的内容:

function(req,res){
      res.send(req.body);

      //req.body.email would correspond with the HTML <input name="email"/>
}

对于那些熟悉PHP的人:为了访问PHP的$_GET变量,我们使用req。为了访问PHP的$_POST变量,我们使用req。Node.js中的body。

后端:

import express from 'express';
import bodyParser from 'body-parser';

const app = express();
app.use(bodyParser.json()); // add a middleware (so that express can parse request.body's json)

app.post('/api/courses', (request, response) => {
  response.json(request.body);
});

前端:

fetch("/api/courses", {
  method: 'POST',
  body: JSON.stringify({ hi: 'hello' }), // convert Js object to a string
  headers: new Headers({ "Content-Type": "application/json" }) // add headers
});

从Express 4.16.0开始,事情再次发生了变化,你现在可以像在Express 3.0中一样使用Express .json()和Express .urlencoded()。

从Express 4.0到4.15,这是不同的:

$ npm install --save body-parser

然后:

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

剩下的就像Express 3.0一样:

首先,您需要添加一些中间件来解析主体的post数据。

添加以下一行或两行代码:

app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies

然后,在处理程序中使用req。体对象:

// assuming POST: name=foo&color=red            <-- URL encoding
//
// or       POST: {"name":"foo","color":"red"}  <-- JSON encoding

app.post('/test-page', function(req, res) {
    var name = req.body.name,
        color = req.body.color;
    // ...
});

注意,不建议使用express.bodyParser()。

app.use(express.bodyParser());

…等价于:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());

express.multipart()存在安全问题,因此最好显式地添加对所需的特定编码类型的支持。如果你确实需要多部分编码(例如支持上传文件),那么你应该阅读这篇文章。

适用于Express 4.1及以上版本

因为大多数答案都是使用Express, bodyParser, connect;其中不建议使用多部分。有一种安全的方法可以轻松地发送post多部分对象。

Multer可以用来代替connect.multipart()。

安装包

$ npm install multer

在你的应用中加载它:

var multer = require('multer');

然后,将它与其他表单解析中间件一起添加到中间件堆栈中。

app.use(express.json());
app.use(express.urlencoded());
app.use(multer({ dest: './uploads/' }));

Connect.json()处理application/json

Connect.urlencoded()处理application/x-www-form-urlencoded

multer()处理多部分/表单数据