Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/huzunjie/nodejs-spider

node 工具们
https://github.com/huzunjie/nodejs-spider

Last synced: 4 days ago
JSON representation

node 工具们

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);
});
};
```