Search code examples
node.jsreactjsrestnodejs-server

Connecting GET request and POST request in NodeJS


I'm still a beginner in NodeJS with little resource for reference. How can I connect this two script together ?

getproductinfo.js perform a GET request to an API endpoints and gets all data needed by Saveproductinfo.js to do a POST request and save all info into database API.

getProductinfo.js

var axios = require('axios');

var config = {
  method: 'get',
  url: 'http://localhost:3001/api/get-products?keywords=shirt&min_sale_price=1&max_sale_price=100&page_no=1',
  headers: { }
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

saveProductinfo.js

    var axios = require('axios');
var qs = require('qs');
var data = qs.stringify({
  'product_id': '1',
  'product_title': 'vxzvxzvxz',
  'promotion_link': 'vxzvxzvxz',
  'sale_price': 'vxzvxzvxz',
  'first_level_category_id': 'vxzvxzvxz',
  'second_level_category_id': 'vxzvxzvxz',
  'second_level_category_name': 'vxzvxzvxz',
  'first_level_category_name': 'vxzvxzvxz',
  'original_price_currency': 'vxzvxzvxz',
  'lastest_volume': 'vxzvxzvxz',
  'shop_url': 'vxzvxzvxz',
  'product_detail_url': 'vxzvxzvxz',
  'original_price': 'vxzvxzvxz',
  'product_main_image_url': 'vxzvxzvxz',
  'product_small_image_urls': 'vxzvxzvxz' 
});
var config = {
  method: 'post',
  url: 'http://localhost:3000/api/product',
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Solution

  • You can do something like this:

    var axios = require('axios');
    
    var qs = require('qs');
    var data = qs.stringify({
      'product_id': '1',
      'product_title': 'vxzvxzvxz',
      'promotion_link': 'vxzvxzvxz',
      'sale_price': 'vxzvxzvxz',
      'first_level_category_id': 'vxzvxzvxz',
      'second_level_category_id': 'vxzvxzvxz',
      'second_level_category_name': 'vxzvxzvxz',
      'first_level_category_name': 'vxzvxzvxz',
      'original_price_currency': 'vxzvxzvxz',
      'lastest_volume': 'vxzvxzvxz',
      'shop_url': 'vxzvxzvxz',
      'product_detail_url': 'vxzvxzvxz',
      'original_price': 'vxzvxzvxz',
      'product_main_image_url': 'vxzvxzvxz',
      'product_small_image_urls': 'vxzvxzvxz' 
    });
    
    var productDetails = {};
    async function getProductInfo() {
      var config = {
        method: 'get',
        url: 'http://localhost:3001/api/get-products?keywords=shirt&min_sale_price=1&max_sale_price=100&page_no=1',
        headers: { }
      };
      try {
        const result = axios(config);
        productDetails = result;
      } catch (error) {
        console.error(error);
      }
    }
    
    async function Saveproductinfo() {
      // before creating config data you can 
      // create data using productDetails and then save it
     // taking values based on schema you shared in the comment
      data = qs.stringify({
      'product_id': '1',
      'product_title': productDetails[0].product_title,
      'promotion_link': 'productDetails[0].promotion_link',
      'sale_price': 'productDetails[0].sale_price',
      'first_level_category_id': 'productDetails[0].first_level_category_id', // do similary for all properties
      'second_level_category_id': 'vxzvxzvxz',
      'second_level_category_name': 'vxzvxzvxz',
      'first_level_category_name': 'vxzvxzvxz',
      'original_price_currency': 'vxzvxzvxz',
      'lastest_volume': 'vxzvxzvxz',
      'shop_url': 'vxzvxzvxz',
      'product_detail_url': 'vxzvxzvxz',
      'original_price': 'vxzvxzvxz',
      'product_main_image_url': 'vxzvxzvxz',
      'product_small_image_urls': 'vxzvxzvxz' 
    });
      var config = {
        method: 'post',
        url: 'http://localhost:3000/api/product',
        headers: { 
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data : data
      };
      
      try {
        const results = await axios(config)
      } catch (e) {
        console.error(`Error is ${e}`);
      }
    }
    
    module.exports = {getProductInfo, Saveproductinfo}
    

    Now, you have two methods that can be unit tested well. You can import them and run them sequentially using async/await.

    const {getProductInfo, Saveproductinfo } = require('filepath');
    
    await getProductInfo(); // await here will work if node v14 >
    await Saveproductinfo();