英文:
Create an Array of Strings in Node from a URL
问题
我想从以下URL获取文本内容:
https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt
然后创建一个名为httpProxies的数组,其中URL中的每一行代表数组中的一个值。
基本上我想要:
console.log(httpProxies); // 返回 [ '78.110.195.242:7080', '144.86.187.56:8888', '64.225.4.85:9999', '34.146.64.228:3128', ...]
我已经开始使用这段代码:
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let data = '';
// 收到数据块。
resp.on('data', (chunk) => {
data += chunk;
});
// 完整的响应已经接收到。打印结果。
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
};
(async (url) => {
console.log(await getScript(url));
})('https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt');
但我不确定如何创建数组。有什么想法吗?
英文:
I want to take the txt contents of this URL:
https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt
And create an array named httpProxies where each line from the URL represents a value in the array.
Basically I want:
console.log(httpProxies); //to return [ '78.110.195.242:7080', '144.86.187.56:8888', '64.225.4.85:9999', '34.146.64.228:3128', ...]
I began using this code:
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
};
(async (url) => {
console.log(await getScript(url));
})('https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt');
But I am unsure how to create the array. Any ideas?
答案1
得分: 3
以下是翻译好的部分:
您可以像这样做:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const getScript = async (url) => {
const response = await fetch(url);
const data = await response.text();
return data.split("\n");
};
(async () => {
const httpProxies = await getScript(
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt"
);
console.log(httpProxies);
})();
<!-- end snippet -->
---
如果您想知道如何在您的代码中执行此操作,只需执行以下操作 `resolve(data.split('\n'));`
```js
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf('https') === 0) {
client = https;
}
client
.get(url, (resp) => {
let data = '';
// 收到数据块。
resp.on('data', (chunk) => {
data += chunk;
});
// 已接收到整个响应。打印结果。
resp.on('end', () => {
resolve(data.split('\n')); // 🔈 这里
});
})
.on('error', (err) => {
reject(err);
});
});
};
(async (url) => {
console.log(await getScript(url));
})(
'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt'
);
<details>
<summary>英文:</summary>
You can do something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const getScript = async(url) => {
const responce = await fetch(url);
const data = await responce.text();
return data.split("\n");
};
(async() => {
const httpProxies = await getScript(
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt"
);
console.log(httpProxies);
})();
<!-- end snippet -->
---
And if you want to know how to do it with your code then just do this `resolve(data.split('\n'));`
```js
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https');
let client = http;
if (url.toString().indexOf('https') === 0) {
client = https;
}
client
.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data.split('\n')); // 👈 This
});
})
.on('error', (err) => {
reject(err);
});
});
};
(async (url) => {
console.log(await getScript(url));
})(
'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt'
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论