这是我的简单表单:

<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调用时,它可以工作,所以。任何想法?


当前回答

你使用了错误的方法req.query.post使用method=get, method=post使用body-parser。

只需尝试通过更改post来获得:

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

在快速代码中使用" app。get "

其他回答

后端:

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
});

你使用了错误的方法req.query.post使用method=get, method=post使用body-parser。

只需尝试通过更改post来获得:

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

在快速代码中使用" app。get "

Post参数可通过以下方式检索:

app.post('/api/v1/test',Testfunction);
http.createServer(app).listen(port, function(){
    console.log("Express server listening on port " + port)
});

function Testfunction(request,response,next) {
   console.log(request.param("val1"));
   response.send('HI');
}

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

给定某种形式:

<form action='/somepath' method='post'>
   <input type='text' name='name'></input>
</form>

使用快递

app.post('/somepath', function(req, res) {

    console.log(JSON.stringify(req.body));

    console.log('req.body.name', req.body['name']);
});

输出:

{"name":"x","description":"x"}
req.param.name x