My code,I am new to Express.
app.get('/', (req, res) => {
const name = req.query.name;
const rank = req.query.rank;
const headers = Object.entries(req.headers)
.map(([key, value]) => `${key}: ${value}`);
res.json({ success: true, data : { name, rank } , headers : headers });
});
server = app.listen(3000);
const res = axios.get('http://localhost:3000?name=John Malone&rank=Captain');
console.log(res);
Terminal output
Promise { <pending> }
I changed my code,data output
data: {
success: true,
data: { name: 'John Malone', rank: 'Captain' },
headers: [
'accept: application/json, text/plain, */*',
'user-agent: axios/0.20.0',
'host: localhost:3000',
'connection: close'
]
}
}
axios.get('http://localhost:3000?name=John Malone&rank=Captain')
.then(function(data) {
console.log(data.data.name); // Data response from server
});
Shows undefined.