无法在POST请求上获取表单数据

时间:2019-02-05 09:36:32

标签: javascript node.js body-parser

我试图获取我的帖子请求的参数。我可以使用JSON发送它们,并且可以工作(如果我取出BodyParser.json的type属性),但不能使用表单数据。我使用如下的body-parser中间件。

const BodyParser      = require('body-parser')

const Config          = require('../config/environment');

const Express         = require("express");
const App             = Express();

App.use(BodyParser.json({type: '/', limit: '50mb'}));
App.use(BodyParser.urlencoded({extended: false}));

App.listen(3000, () => {Response.logger('Api running on port 3000.');});  

App.post("/signup", (req, res, next) =>
{
    consoleAlert('SIGNUP', false);

    console.log(req);

    Account.signup(req.params).then(
    function(results) {response(results, res, 'SIGNUP');},
    function(error)   {response(error, res, 'SIGNUP');});
});  

因此,当我打印出要求时,正文始终为空,其中包含表单数据

1 个答案:

答案 0 :(得分:0)

从头开始编写-看来可行:

服务器:

//app.js
const express = require('express');
const bodyParser = require('body-parser');

let app = express();

app.use(bodyParser.urlencoded({extended: false}));

app.post('/', function(req, res, next) {
    console.log(req.body);
});

app.listen(3022);

客户端:从命令行调用curl发送发送表单数据(默认为application / x-www-form-urlencoded),我的节点服务器IP为10.10.1.40:

curl -d "param1=value1&param2=value2" -X POST http://10.10.1.40:3022/
相关问题