代理错误:无法将请求 /users 从 localhost:3000 代理到 http://localhost:3001/

ASJ

我正在将一些数据从我的 ReactJS 前端应用程序发送到我的节点/快速后端,但是,每当我发送数据时,我都会收到标题中提到的错误消息。

这是我的 ReactJS 代码:

class App extends React.Component {

constructor(props) {
    super(props);
    //states and binding functions
}

fetchData () {
    fetch('/users')
        .then(res => res.json())
        .then(users => this.setState({users}));
}

addUser (event) {

    let fileData = new FormData();

    fileData.append("file", this.state.filesToBeSent);

    axios.post('adduser', 
        querystring.stringify({
          entry: this.state.currname,
          passwrd: this.state.currpasswrd,
          fileData : fileData
        })
            )
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });

}

componentDidMount() {
    this.fetchData();
}

render() {
    return (
        <div className="App">
            <form onSubmit={this.addUser} encType="multipart/form-data">
                <label>New username:</label>
                <input value={this.state.currname} onChange={this.handleChange}></input>
                <input value={this.state.currpasswrd} onChange={this.handlePassword} />
                <input type="file" name="file" onChange={this.handleFile} />
                <button type="submit" >Add</button>
            </form>

            <h1>Current Users: </h1>
                {this.state.users.map((name, n) =>
                    //map through user-array and display names
                )}
            </ul>
        </div>
    );
}

}

(对不起,如果代码很多,我尽可能地缩短了它,但我不确定哪些部分与问题相关)。

以下是我如何在节点中接收数据以及如何将部分数据保存到我的数据库中:

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, "./uploads/");
    },

    filename: (req, file, cb) => {
        const newFilename = `${uuidv4()}${path.extname(file.originalname)}`;
        cb(null, newFilename);
    },
})

const upload = multer({ storage });

router.post("/", upload.single("file"), (req, res) => {
    res.send(req.file + " and exit.");
    var newUser = new Todo();
        newUser.username = req.body.entry;
        newUser.passwrd = req.body.passwrd;
        newUser.save(function (err) {
            if(err) 
                res.send(err);
            res.send('User added successfully!');
        });
});

这就是它变得奇怪的地方。该应用程序运行良好,直到我插入upload.single("file"),但是,我似乎无法弄清楚原因。当我只有文本输入时,我没有任何问题,即使当我创建FormData()等时,它仍然可以正常工作,直到我实现它。

我尝试查找它并实施此处发布的答案,但是,似乎没有任何帮助。

在屏幕上,我收到以下错误消息:Unhandled Rejection (SyntaxError): Unexpected token P in JSON at position 0当我检查终端时,我收到标题中提到的错误消息。我尝试删除内容标题(不确定那会做什么,但我遵循的实现文件上传的教程没有使用内容标题,这就是我尝试删除它们的原因。有谁知道如何解决这个错误?

编辑:终端中的错误消息还包含ECONNRESET. 我按照终端https://nodejs.org/api/errors.html#errors_common_system_errors 中的链接进行操作,但我仍然不确定如何解决该问题。

优尼

我建议您将所有字段附加到FormData对象,并且不要将提交的数据转换为 json 字符串:

let fileData = new FormData();

fileData.append("entry", this.state.currname);
fileData.append("passwrd", this.state.currpasswrd);
fileData.append("file", this.state.filesToBeSent);

axios.post('adduser', fileData)
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

代理错误:无法将请求/付款从localhost:3000代理到https:// localhost:5000 /

来自分类Dev

Ember CLI和Rails:代理到http:// localhost:3000时出错

来自分类Dev

ruby - 运行 http://localhost:3000/admin/users 但redirect_to http://localhost:3000/admin/login

来自分类Dev

身份验证POST http:// localhost:3000 / api / users / register 400(错误请求)MERN应用

来自分类Dev

无法访问 http://localhost:3000/

来自分类Dev

Rails 5 + Foreman + Pow给我“无法将请求代理到localhost:5000”

来自分类Dev

使用 localhost 作为 http 代理

来自分类Dev

使用代理目标将用户从localhost:3000移至localhost:3000 / auth / google

来自分类Dev

$ .getJSON将'http:// localhost:3000'添加到api URL

来自分类Dev

不允许请求来源:使用Rails5和ActionCable时的http:// localhost:3001

来自分类Dev

如何从user_path(:current)获取“ http:// localhost:3000 / users / current”?

来自分类Dev

POST http:// localhost:3000/404(未找到)

来自分类Dev

http:// localhost:3000 / api / stuff的Http失败响应:400错误的请求

来自分类Dev

在端口3000或3001上访问localhost时返回404(BrowserSync使用的默认端口)

来自分类Dev

NGINX反向代理到HTTP

来自分类Dev

将localhost:3280 / some / long / url镜像到localhost:3000

来自分类Dev

尝试代理到以下主机时发生错误:localhost:4200 / api / v1 / generate_uid

来自分类Dev

错误:正在加载http:// localhost:3000 / rxjs / RX(…)的XHR错误(找不到404)

来自分类Dev

在http:// localhost:3000 / entries / view_all上引发错误

来自分类Dev

XHR错误(找不到404)载入http:// localhost:3000 / @ angular / core

来自分类Dev

启用了热模块替换,但无法正常使用http:// localhost:3000 / __ webpack_hmr

来自分类Dev

添加GPG密钥的'无效的HTTP代理(http:// localhost:4001):错误的URI'错误

来自分类Dev

POST http://localhost:3000/signin 400 (Bad Request) in react

来自分类Dev

使用PUMA的Rails,将localhost:3000更改为localhost:3000 / example

来自分类Dev

Express.js 可以将 localhost:3000 和 localhost:3000/ 设置为不同的路由吗?

来自分类Dev

在 React 中将所有内容放在 http://localhost:3000/app 而不是 http://localhost:3000/ 下的最简单方法

来自分类Dev

无法启动“curl:localhost:3000”端口,显示 URI 错误

来自分类Dev

骆驼Http代理到Web服务

来自分类Dev

Wget命令错误(解析代理URL http:// localhost:4001时出错:端口号错误。)

Related 相关文章

  1. 1

    代理错误:无法将请求/付款从localhost:3000代理到https:// localhost:5000 /

  2. 2

    Ember CLI和Rails:代理到http:// localhost:3000时出错

  3. 3

    ruby - 运行 http://localhost:3000/admin/users 但redirect_to http://localhost:3000/admin/login

  4. 4

    身份验证POST http:// localhost:3000 / api / users / register 400(错误请求)MERN应用

  5. 5

    无法访问 http://localhost:3000/

  6. 6

    Rails 5 + Foreman + Pow给我“无法将请求代理到localhost:5000”

  7. 7

    使用 localhost 作为 http 代理

  8. 8

    使用代理目标将用户从localhost:3000移至localhost:3000 / auth / google

  9. 9

    $ .getJSON将'http:// localhost:3000'添加到api URL

  10. 10

    不允许请求来源:使用Rails5和ActionCable时的http:// localhost:3001

  11. 11

    如何从user_path(:current)获取“ http:// localhost:3000 / users / current”?

  12. 12

    POST http:// localhost:3000/404(未找到)

  13. 13

    http:// localhost:3000 / api / stuff的Http失败响应:400错误的请求

  14. 14

    在端口3000或3001上访问localhost时返回404(BrowserSync使用的默认端口)

  15. 15

    NGINX反向代理到HTTP

  16. 16

    将localhost:3280 / some / long / url镜像到localhost:3000

  17. 17

    尝试代理到以下主机时发生错误:localhost:4200 / api / v1 / generate_uid

  18. 18

    错误:正在加载http:// localhost:3000 / rxjs / RX(…)的XHR错误(找不到404)

  19. 19

    在http:// localhost:3000 / entries / view_all上引发错误

  20. 20

    XHR错误(找不到404)载入http:// localhost:3000 / @ angular / core

  21. 21

    启用了热模块替换,但无法正常使用http:// localhost:3000 / __ webpack_hmr

  22. 22

    添加GPG密钥的'无效的HTTP代理(http:// localhost:4001):错误的URI'错误

  23. 23

    POST http://localhost:3000/signin 400 (Bad Request) in react

  24. 24

    使用PUMA的Rails,将localhost:3000更改为localhost:3000 / example

  25. 25

    Express.js 可以将 localhost:3000 和 localhost:3000/ 设置为不同的路由吗?

  26. 26

    在 React 中将所有内容放在 http://localhost:3000/app 而不是 http://localhost:3000/ 下的最简单方法

  27. 27

    无法启动“curl:localhost:3000”端口,显示 URI 错误

  28. 28

    骆驼Http代理到Web服务

  29. 29

    Wget命令错误(解析代理URL http:// localhost:4001时出错:端口号错误。)

热门标签

归档