Created
June 25, 2018 13:40
-
-
Save tuor4eg/9a3941aa3c1e4ecd80226fd97493a46b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (queryPar) => { | |
const postData = querystring.stringify(queryPar.data); | |
const { protocol, hostname, pathname, port, query } = url.parse(queryPar.url, true); | |
const options = { | |
protocol, | |
host: hostname, | |
path: `${pathname}${getSearch(query, queryPar.params)}`, | |
port, | |
method: queryPar.method, | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(postData) | |
}, | |
}; | |
return new Promise((resolve, reject) => { | |
const req = http.request(options, (res) => { | |
const body = []; | |
res.on('data', chunk => { | |
body.push(chunk.toString()); | |
}).on('end', () => { | |
const html = body.join(''); | |
const getData = { | |
status: res.statusCode, | |
data: html, | |
} | |
resolve(getData); | |
}); | |
}); | |
req.on('error', err => { | |
reject(err); | |
}); | |
if (postData) { | |
req.write(postData); | |
} | |
req.end(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment