简体   繁体   中英

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:

 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). I have tried all variation possible : "./users", "../users", "#/users" etc...

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:

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

更新服务器后刷新状态

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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