【问题标题】:express body-parser returns empty body in req objectexpress body-parser 在 req 对象中返回空正文
【发布时间】:2019-02-13 13:32:38
【问题描述】:

我是反应和节点的初学者,这将是一个非常基本的问题。 我正在创建一个带有节点后端的基本反应。我设置了mysql数据库连接并设置好了。我想使用 /createUser api 调用插入用户详细信息以将数据存储到数据库。 我运行 server/app.js、react serve 和 index.js,其中包含我的 /createUser 监听器。 当我使用我的表单输入用户名、密码时,空的 req.body 对象将在 express.post 方法中返回,而我期待输入的用户名和密码。 在其他情况下,当我启动侦听器时,通过给出以下错误来响应前端未加载。

GET http://localhost:3000/%PUBLIC_URL%/manifest.json 404(未找到) manifest.json:1 Manifest: Line: 1, column: 1, Unexpected token.

似乎节点组件无法访问我的清单文件。我说的对吗?

index.js

const express = require('express');
const bodyParser = require('body-parser');
const store = require('./store');
const app = express();
app.use(express.static('public'));

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: 'application/*+json' }));

app.use(bodyParser.json());

app.post('/createUser', (req, res) => {
    console.log(req.body);
    store
        .createUser({
            username: req.body.username,
            password: req.body.password
        })
        .then(() => res.sendStatus(200))
})

app.listen(3001, () => {
    console.log('Server running on http://localhost:3001')
})

login.js

import React, { Component } from 'react';
import classes from './Login.css';

function post (path, data) {
    console.log(data);
    return window.fetch(path, {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
}

function handleLoginClick(e) {
    e.preventDefault();
    console.log('The link was clicked.');

    const Login = document.querySelector('.Login');
    const username = Login.querySelector('.username').value;
    const password = Login.querySelector('.password').value;
    post('/login', { username, password })
        .then(({ status }) => {
            if (status === 200) alert('login success')
            else alert('login failed')
        })
}

function handleSignupClick(e) {
    e.preventDefault();
    console.log('The link was clicked sign up.');

    const CreateUser = document.querySelector('.CreateUser')
    const username = CreateUser.querySelector('.username').value;
    const password = CreateUser.querySelector('.password').value;
    post('/createUser', { username, password })
}


class Login extends Component {

    componentDidMount() {
    }

    componentWillUnmount() {
    }

    render() {
        return (
            <div>
                <form className="Login">
                    <h1>Login</h1>
                    <input type="text" className="username" placeholder="username"/>
                    <input type="password" className="password" placeholder="password"/>
                    <input  type="submit" value="Login" onClick={handleLoginClick}/>
                </form>
                <form className="CreateUser">
                    <h1>Create account</h1>
                    <input type="text" className="username" placeholder="username"/>
                    <input type="password" className="password" placeholder="password"/>
                    <input type="submit" value="Create" onClick={handleSignupClick}/>
                </form>
            </div>
        );
    }

}
export default Login;

我的代码有什么问题?谁能解释一下。

我的代码:https://github.com/indunie/tms2

【问题讨论】:

  • 你能把 app.use(bodyParser.json({ type: 'application/*+json' })); 行去掉吗?并尝试?
  • 是的,我之前做过,在阅读了图书馆的一些文件后添加了它。添加后没有任何反应。
  • 您是否尝试通过邮递员或其他类似应用发布 API?只是看看问题出在后端还是前端?
  • 是的,我也尝试过使用邮递员。在前端,数据成功发送到后端。邮递员返回OK。如果我返回响应,它也会{}

标签: javascript node.js reactjs express body-parser


【解决方案1】:

尝试将 bodyParser 配置替换为以下代码行:

app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ limit: '100mb', extended: false }));

这可能对你有帮助

【讨论】:

  • 感谢您的建议。添加上述限制后也会发生同样的情况。无论如何,我们为什么要使用这个限制?
【解决方案2】:

已解决 [https://github.com/expressjs/express/issues/3881][1]

这里有两种解决方案:

  1. 将 URL 更改为 http://localhost:3001/manifest.json 以匹配您设置 express.static 的方式。
  2. express.static 更改为app.use('/public', express.static('public'));,这样http://localhost:3001/public/manifest.json 将引用给定的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多