1

I am trying to pass the generalDetail data from my react front end to my node server. I am currently getting a connection refused error.

(OPTIONS http://localhost:5000/api/home net::ERR_CONNECTION_REFUSED).

here is my onSubmitForm function:

  onSubmitForm(e){
  e.preventDefault();

  let data = {
            generalDetail: this.state.generalDetails,
            firstName: this.state.firstName,
            middleName: this.state.middleName,
            lastName: this.state.lastName
      };

      axios.post("http://localhost:5000/home", data).then(() => {

       }).catch(() => {
          console.log("Something went wrong. Plase try again later");
      });


}
  //end

  onContentChange(fieldname, data){

    console.log('On Content Change', data);

     this.setState({
       [fieldname]: data

    });
  }



}

Here is my server.js file

const express = require('express');

const app = express();


// http://localhost:5000/api/home

app.post('/api/home', (req, res) => {
  const data = [
    {req.body.generalDetail},
  ];

  res.json(data);
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);
2
  • 1
    you are declaring your api route as '/api/home' but sending the data to '/home'. You need to make sure you are sending data to the correct route. Commented May 14, 2019 at 2:37
  • if you change your .catch to handle the error you will see that you are getting a 404. try using: .catch( (error) => { console.log(error); )); Commented May 14, 2019 at 2:39

3 Answers 3

1

You can change your code into this

Example

onSubmitForm = e => {
        e.preventDefault();
        let data = {
              generalDetail: this.state.generalDetails,
              firstName: this.state.firstName,
              middleName: this.state.middleName,
              lastName: this.state.lastName
        };

        axios.post("http://localhost:5000/api/home", data).then(() => {
           //do something
         }).catch(() => {
            console.log("Something went wrong. Plase try again later");
        });
    }

Sign up to request clarification or add additional context in comments.

Comments

1

try this

const express = require('express')
const app = express()
const port = 8000 //your port number

const cors = require('cors')
app.use(cors())
var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

8 Comments

so did you see "Server running on port ${port}" in you console
No I didn't actually haha
No it isn't unfortunately
did u start the server. "node server.js" file . Did u get any errors?
its saying invalid syntax for {req.body.generalDetail}, Would you happen to know the correct syntax? I'm trying to pass the data through from react to the backend.
|
0

Try to add cors preflight code in your backend code (server.js).

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS");
  res.header("Access-Control-Allow-Credentials", true);
  next();
});
app.post('/api/home', (req, res) => {
  const data = [{ generalDetails: req.body.generalDetail }];
  res.json(data);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.