Created
July 6, 2018 10:23
-
-
Save tuor4eg/fc05caaa6c4dc4232ec4b79dbd8cdadc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| БЫЛО: | |
| const getToken = body => body.match(/value="(\w+)"/)[1]; | |
| export default (registrationFormUrl, submitFormUrl, nickname, callback) => { | |
| // BEGIN (write your solution here) | |
| const {protocol, hostname, path, pathname, port} = url.parse(submitFormUrl); | |
| http.get(registrationFormUrl, res => { | |
| const body = []; | |
| res.on('data', chunk => { | |
| body.push(chunk.toString()); | |
| }).on('end', () => { | |
| if (res.statusCode !== 200) { | |
| callback(new Error(res.statusCode)); | |
| return; | |
| } | |
| const html = body.join(''); | |
| const postData = querystring.stringify({ nickname, token: getToken(html) }); | |
| const options = { | |
| host: hostname, | |
| path: path, | |
| port: port, | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| 'Content-Length': Buffer.byteLength(postData) | |
| } | |
| }; | |
| const req = http.request(options, (result) => { | |
| if (result.statusCode !== 302) { | |
| callback(new Error(result.statusCode)); | |
| return; | |
| } | |
| callback(null); | |
| }); | |
| req.end(postData); | |
| }); | |
| }); | |
| // END | |
| }; | |
| СТАЛО: | |
| export default (registrationFormUrl, submitFormUrl, nickname) => { | |
| const doIt = async () => { | |
| const response = await get(registrationFormUrl); | |
| if (response.status !== 200) { | |
| return (new Error(response.status)); | |
| } | |
| const postData = await post(submitFormUrl, {nickname, token: getToken(response.data)}); | |
| if (postData.status !== 302) { | |
| return (new Error(postData.status)); | |
| } | |
| return null; | |
| }; | |
| return doIt(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment