javascript - Express 主体解析器在 req 对象中返回空主体

标签 javascript node.js reactjs express body-parser

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

获取http://localhost:3000/%PUBLIC_URL%/manifest.json 404(未找到) manifest.json:1 list :行:1,列:1,意外标记。

Node 组件似乎无法访问我的 list 文件。我说得对吗?

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

登录.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

最佳答案

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

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

这可能对你有帮助

关于javascript - Express 主体解析器在 req 对象中返回空主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54655822/

相关文章:

javascript - 根据下拉值加载 html 表单?

javascript - React Bit Dev 模块显示 404

javascript - 延迟键盘关闭 react native

javascript - 在 Kendo UI Window 控件中解析 JSON

javascript - 警告 : Failed prop type: Invalid prop `children` supplied to `j`

javascript - 如何通过在一页上获取 url 页面来更改事件的背景菜单

javascript - Mongoose:嵌套 Json 的架构并存储它 (Node.js)

javascript - node.js 非阻塞 POST 请求等待另一个 POST 请求

node.js - 在生产中禁用 EJS 缓存

jquery - react 日期选择器: Highlight current month's days