Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huzunjie/nodejs-spider
node 工具们
https://github.com/huzunjie/nodejs-spider
Last synced: 4 days ago
JSON representation
node 工具们
- Host: GitHub
- URL: https://github.com/huzunjie/nodejs-spider
- Owner: huzunjie
- Created: 2014-11-24T05:10:28.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-11-17T02:43:17.000Z (almost 6 years ago)
- Last Synced: 2023-08-02T23:10:38.168Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nodejs-spider
=============```js
import fs from 'fs';
import https from 'https';function writeFilePromise(file, data){
return new Promise((resolve, reject) => {
fs.writeFile(file, data, err => {
err ? reject(err) : resolve(file);
});
});
}function getContentPromise(url){
return new Promise((resolve, reject) => {
https.get(url, res => {
var chunks = [], size = 0;
res.on("data" , chunk => {
chunks.push(chunk);
size += chunk.length;
});
res.on("end" , () => {
if(res.statusCode !== 200){
return reject({ code:res.statusCode, errorMessage:'HTTP Error' });
}
var data = Buffer.concat(chunks , size).toString();
resolve(data);
})
}).on('error' , reject);
});
};
```