安装axios
npm install axios
config.js
module.exports = {
//请求地址
URL:'https://127.0.0.1',
//请求次数 修改后可不重新生成文件
REQUEST_NUM:1,
//并发次数 修改后需要重新生成文件
CONCURRENT_NUM:10,
//请求延迟时间 修改后可不重新生成文件
INTERVAL_TIME:0
};
setfile.js
const fs = require('fs') // 引入fs模块
const config = require('./config');
let data =
`const axios = require('axios');
const qs = require('qs');
const path = require('path');
const config = require('./config');
let request_num = 0
`
let fd = ``
for (let index = 0; index < config.CONCURRENT_NUM; index++) {
fd+= `test${index}(),`
data+=`
async function test${index}() {
return await axios({
method: 'GET',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
url: config.URL,
data: '',
}).then(res=>{
console.log('request success...');
// console.log(res.data);
})
}
`
}
fd = fd.substr(0, fd.length - 1);
data+=`
let interval = setInterval(() => {
if(request_num == config.REQUEST_NUM){
clearInterval(interval);
return false
}
axios
.all([${fd}])
.then(axios.spread(function (acct, perms) {
//console.log(acct);
console.log('\x1B[33m%s\x1b[0m','REQUEST_NUM:${config.REQUEST_NUM} CONCURRENT_NUM:${config.CONCURRENT_NUM} INTERVAL_TIME:${config.INTERVAL_TIME}s');
}));
request_num+=1
}, config.INTERVAL_TIME);
`
fs.writeFile('./index.js', data, function(err) {
if(err) {
throw err
}
console.log('The file is ready')
// fs.readFile('./index.js', 'utf-8', function(err, data) {
// if(err) {
// throw err
// }
// console.log('index.js的内容: ', data)
// })
})
package.json 新增scripts指令
"scripts": {
"start": "node ./setfile.js & node ./index.js"
},
执行
npm run start
//or
yarn start