ramazan793
ramazan793

Reputation: 689

Getting axios data from post request in my node-js back-end

I send a post request in my vue component file :

axios.post('/add-couple', {
    word_key: this.word_key,
    word_value: this.word_value
  })
  .then((response) => {
    this.word_key = ''
    this.word_value = ''
  })

And handle that in dev-server.js(using express):

app.post('/add-couple', (req,res) => {
  newCouple(req.word_key,req.word_value)
  console.log(req.word_key,req.word_value) //undefined undefined 
  res.end()
})

So, i want to use word_key and word_value vars, but cant, because they're both undefined. What am i doing wrong?

Upvotes: 2

Views: 3332

Answers (1)

alexmac
alexmac

Reputation: 19607

You should use body-parser middleware and req.boby object to get sent params:

var bodyParser = require('body-parser');

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

app.post('/add-couple', (req, res) => {
  console.log(req.body.word_key, req.body.word_value);
  ...
});

Upvotes: 2

Related Questions