Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling POST request with axios and express

I can't seem to figure out why when I'm doing an axios.post method providing a body of data, it gets captured as undefined on my server.

I have the following files with snippets of code:

app.js:

auth(user, pass){
    return axios.post('http://localhost:3000/auth', {
        username: user, 
        password: pass
    })
}

server.js:

app.post('/auth', (req, res) => {
    console.log(req.body) //undefined
    res.end("Success")  
})

I'm how can I properly handle the POST data using axios? Am I missing something?

The success returns fine, but the username / password cannot seem to be found anywhere in the req

like image 975
Magnum Avatar asked May 31 '16 16:05

Magnum


1 Answers

Add following parser to handle the requested data

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
like image 92
Weijing Jay Lin Avatar answered Oct 14 '22 15:10

Weijing Jay Lin