0

I'm trying to implement this little login system of mine, and I have a form that sends a post request to my webpack dev server, which in turn proxies the request to my server.

This is the function that handles the form submit and sends a POST request to the server.

handleSubmit = (e) => {
  e.preventDefault();
  this.props.form.validateFields((err, values) => {
    if (!err) {
      console.log('Received values of form: ', values);
      fetch('/login', {
        method: 'POST',
        body: values
      });
    }
  });
}

When I click Login, I do get my values logged in the console as shown here http://prntscr.com/hsbpyp

This is the server side code which handles the login endpoint

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/login', (req, res) => {
  console.log('Got user information: \n', req.body);
  res.send('asd');
})

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));

app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));



app.listen(3001, () => {
  console.log(`
    =====
    Server running at http://localhost:3001/
    GraphiQL running at http://localhost:3001/graphiql/
    =====
  `)
})

As you can see, I'm indeed using the body-parser middleware to parse the req.body but when I attempt to log the body, I get an empty object http://prntscr.com/hsbqrd

Can someone help me figure out what is going on with my code?

JJJ
  • 32,902
  • 20
  • 89
  • 102
buoyantair
  • 347
  • 4
  • 11

3 Answers3

0

Maybe on your client side, you should try with this method ?

var values = {
    username: 'test',
    password: 'test'
};

fetch("/login", {
   method: "POST",
   headers: {
     'Accept': 'application/json, text/plain, */*',
     'Content-Type': 'application/json'
   },
   body: JSON.stringify(values)
});

More information here : Fetch Post JSON data

EDIT

I think you also have to configure bodyParser with json data like this :

app.use(bodyParser.json({limit: '200mb'}));

Hope it helps.

Sparw
  • 2,688
  • 1
  • 15
  • 34
0

Try this ajax request with adding JQuery script, I ran your server and the problem was the request not the server, I'm not that good with HTML requests I even tried the MDN examples but they didn't work, this code worked well with your server:

$.post("/login",
{
    name: "name",
    pass: "password"
},
function(data, status){
    console.log(data)
    console.log(status)
});

and this is the script you need:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Ashar Dweedar
  • 613
  • 6
  • 16
0

The request is being sent to the server as multipart/form-data. body-parser does not support multipart/form-data. If indeed you require the POST request to be in this format then have a look at multer or formidable.

Faz
  • 360
  • 3
  • 11
  • How do you know if it's a multipart/form-data ? – buoyantair Dec 26 '17 at 10:19
  • The screenshot that you show here http://prntscr.com/hsbxh4 suggests it is multipart/form-data. If you inspect or output the Content-Type request header you will see the format it is being sent as. You can build the request body yourself as json and send as json in which case you will need to add the bodyParser.json() to the login route handler. Or modify your form to submit as urlencoded which is the body-parser you are currently attempting with. – Faz Dec 26 '17 at 10:58
  • Sorry for the delay, I'll look into this. Thank you – buoyantair Dec 26 '17 at 12:12