0

I use axios.get() send data to server nodejs, i use request.body to use but request.body return {} for me.

in server.js file

const express = require('express');
const bodyParser = require('body-parser');
const app = express();


const setting = {
  port : 20101
}

app.set('port', (process.env.PORT || setting.port));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function(req, res, next){
  res.setHeader('Access-Control-Allow-Origin','*');
  res.setHeader('Cache-Control', 'no-cache');
  next();
});

app.get('/search', function(req, res){
   if(!req.body) return res.sendStatus(400);
   console.log(req.body, ' <--- req.body'); // return {} <--- req.body
});


app.listen(setting.port, function() {
   console.log('listening on port : ' + setting.port);
});

and api file

 axios.get(apiPath('/search'),{
    keyword
  })
  .then(function(response){
    console.log(response, '<--- response');
  })
  .catch(function(error){
    console.log(error, '<--- error');
  })

Please suggest how I should solve the problem.

jupeter
  • 746
  • 4
  • 18

1 Answers1

2

You used Get method..Change it to Post method

app.post('/search', function{});
Janen R
  • 729
  • 10
  • 21
  • Its already in stackoverflow https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js...here all the suggestions are perfect to explain in beginner way..then u may try to join mongodb online free course for nodejs developers..`university.mongodb.com`..Im learning this may month session..Thats a good tutorial – Janen R Jun 07 '17 at 10:46