在 NodeJS 中连接 GET 请求和 POST 请求
Connecting GET request and POST request in NodeJS
我仍然是 NodeJS 的初学者,可供参考的资源很少。
如何将这两个脚本连接在一起?
getproductinfo.js
对 API 端点执行 GET 请求并获取 Saveproductinfo.js
执行 POST 请求所需的所有数据并将所有信息保存到数据库 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);
});
如果您导出 POST 函数以便可以在其他地方的代码中访问它,您应该能够调用 GET
并在 then() 回调中调用 post,像这样:
首先修改你的POST函数接受数据参数:
async function postFunction(data){
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
}
然后用GET函数的结果调用它:
import <yourPostFunction>
var axios = require('axios');
const 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) {
//GET function has finished running
const result=JSON.stringify(response.data); //Might need to await this data
const postResult= await postFunction(result);//call your post function
})
.catch(function (error) {
console.log(error);
});
你可以这样做:
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}
现在,您有两种方法可以很好地进行单元测试。您可以使用 async/await.
顺序导入它们并 运行 它们
const {getProductInfo, Saveproductinfo } = require('filepath');
await getProductInfo(); // await here will work if node v14 >
await Saveproductinfo();
我仍然是 NodeJS 的初学者,可供参考的资源很少。 如何将这两个脚本连接在一起?
getproductinfo.js
对 API 端点执行 GET 请求并获取 Saveproductinfo.js
执行 POST 请求所需的所有数据并将所有信息保存到数据库 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);
});
如果您导出 POST 函数以便可以在其他地方的代码中访问它,您应该能够调用 GET
并在 then() 回调中调用 post,像这样:
首先修改你的POST函数接受数据参数:
async function postFunction(data){
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
}
然后用GET函数的结果调用它:
import <yourPostFunction>
var axios = require('axios');
const 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) {
//GET function has finished running
const result=JSON.stringify(response.data); //Might need to await this data
const postResult= await postFunction(result);//call your post function
})
.catch(function (error) {
console.log(error);
});
你可以这样做:
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}
现在,您有两种方法可以很好地进行单元测试。您可以使用 async/await.
顺序导入它们并 运行 它们const {getProductInfo, Saveproductinfo } = require('filepath');
await getProductInfo(); // await here will work if node v14 >
await Saveproductinfo();