@abhishekrajeshirke/axios-curlirize 中文文档教程
Description
该模块是 axios 第三方模块,用于在控制台中将任何 axios 请求记录为 curl 命令。 它最初作为建议发布在 axios 存储库上,但由于我们认为发布此类功能不在 axios 的范围内,因此我们决定将其作为一个独立的模块。
How it works
该模块利用 axios 的拦截器将请求记录为 cURL 命令。 它还将其存储在响应的配置对象中。 因此,可以在应用程序的控制台以及响应的 res.config.curlCommand
属性中看到该命令。
Changing the logger
默认情况下,axios-curlirize 使用 console.log/error()
函数。 可以通过执行类似以下操作来更改记录器:
// when initiating your curlirize instance
curlirize(axios, (result, err) => {
const { command } = result;
if(err) {
// use your logger here
} else {
// use your logger here
}
});
How to use it
axios-curlirize 非常易于使用。 首先你必须安装它。
npm i --save axios-curlirize@latest
然后您所要做的就是在您的应用程序中导入并实例化 curlirize。 这是一个示例:
import axios from 'axios';
import express from 'express';
import curlirize from 'axios-curlirize';
const app = express();
// initializing axios-curlirize with your axios instance
curlirize(axios);
// creating dummy route
app.post('/', (req, res) => {
res.send({hello: 'world!'})
});
// starting server
app.listen(7500, () => {
console.log('Dummy server started on port 7500')
/*
The output of this in the console will be :
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" --data {"dummy":"data"} http://localhost:7500/
*/
axios.post('http://localhost:7500/', {dummy: 'data'})
.then((res) => {
console.log('success');
})
.catch((err) => {
console.log(err);
});
});
Description
This module is an axios third-party module to log any axios request as a curl command in the console. It was originally posted as a suggestion on the axios repository, but since we believed it wasn't in the scope of axios to release such feature, we decided to make it as an independent module.
How it works
The module makes use of axios' interceptors to log the request as a cURL command. It also stores it in the response's config object. Therefore, the command can be seen in the app's console, as well as in the res.config.curlCommand
property of the response.
Changing the logger
By default, axios-curlirize uses the console.log/error()
functions. It is possible to change the logger by doing something similar to this:
// when initiating your curlirize instance
curlirize(axios, (result, err) => {
const { command } = result;
if(err) {
// use your logger here
} else {
// use your logger here
}
});
How to use it
axios-curlirize is super easy to use. First you'll have to install it.
npm i --save axios-curlirize@latest
Then all you have to do is import and instanciate curlirize in your app. Here's a sample:
import axios from 'axios';
import express from 'express';
import curlirize from 'axios-curlirize';
const app = express();
// initializing axios-curlirize with your axios instance
curlirize(axios);
// creating dummy route
app.post('/', (req, res) => {
res.send({hello: 'world!'})
});
// starting server
app.listen(7500, () => {
console.log('Dummy server started on port 7500')
/*
The output of this in the console will be :
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" --data {"dummy":"data"} http://localhost:7500/
*/
axios.post('http://localhost:7500/', {dummy: 'data'})
.then((res) => {
console.log('success');
})
.catch((err) => {
console.log(err);
});
});