简体   繁体   English

为什么我在与节点反应时对此得到404

[英]Why am I getting a 404 on this in react with node

I am trying to send the data inserted into form to server via ajax request like this: 我试图通过这样的ajax请求将插入表单的数据发送到服务器:

 handleUserSubmit: function(user) {
   var users = this.state.data;
   user.id = Date.now();
   var newUsers = users.concat([user]);
   this.setState({data: newUsers});
   $.ajax({
     url: this.props.url,
     dataType: 'json',
     type: 'POST',
     data: user,
     success: function(data) {
       this.setState({data: data});
     }.bind(this),
     error: function(xhr, status, err) {
       this.setState({data: users});
       console.error(this.props.url, status, err.toString());
     }.bind(this)
   });
 },

and the rendering is done as: 渲染过程如下:

ReactDOM.render(
  <UserBox url="../users" pollInterval={2000} />,
  document.getElementById('data')
);

I am getting this weird console error: 我收到此奇怪的控制台错误:

POST http://localhost:3000/users 404 (Not Found)

Since the "users" file (assuming it's trying to access users.json file is in a directory up one level). 由于“用户”文件(假设它正在尝试访问users.json文件位于上一级目录中)。 I have tried all variation possible : "./users", "../users", "#/users" etc... 我尝试了所有可能的变体:“ ./ users”,“ ../ users”,“#/ users”等...

The code error on this line: 此行的代码错误:

url: this.props.url,

in the code given above. 在上面给出的代码中。 Any idea what's going on? 知道发生了什么吗?

My node.js code is: 我的node.js代码是:

app.post('/users', function(req, res) {
  fs.readFile(USERDATA, function(err, data) {
    if (err) {
      console.error(err);
      process.exit(1);
    }
    var users = JSON.parse(data);
    var newUser = {
      id: Date.now(),
      firstname: req.body.firstname,
      lastname: req.body.lastname,
      address: req.body.address,
      phonenumber: req.body.phonenumber,
      email: req.body.email,
      licensestate: req.body.licensestate,
      licensenum: req.body.licensenum
    };
    console.log(newUser)
    users.push(newUser);
    fs.writeFile(USERDATA, JSON.stringify(users, null, 10),              

function(err) {
      if (err) {
        console.error(err);
        process.exit(1);
      }
      res.json(users);
        });
      });
     }); 

更新服务器后刷新状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM