File tree Expand file tree Collapse file tree 3 files changed +41
-2
lines changed
Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const http = require ( 'http' ) ;
4+
5+ const url = 'http://ietf.org/' ;
6+
7+ http . get ( url , res => {
8+ if ( res . statusCode !== 200 ) {
9+ console . log ( `Status Code: ${ res . statusCode } ` ) ;
10+ return ;
11+ }
12+ res . setEncoding ( 'utf8' ) ;
13+ const lines = [ ] ;
14+ res . on ( 'data' , chunk => {
15+ lines . push ( chunk ) ;
16+ } ) ;
17+ res . on ( 'end' , ( ) => {
18+ console . log ( lines . join ( ) ) ;
19+ } ) ;
20+ } ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const http = require ( 'http' ) ;
4+
5+ const fetch = url => new Promise ( ( resolve , reject ) => http . get ( url , res => {
6+ if ( res . statusCode !== 200 ) {
7+ reject ( `Status Code: ${ res . statusCode } ` ) ;
8+ return ;
9+ }
10+ res . setEncoding ( 'utf8' ) ;
11+ const lines = [ ] ;
12+ res . on ( 'data' , chunk => lines . push ( chunk ) ) ;
13+ res . on ( 'end' , ( ) => resolve ( lines . join ( ) ) ) ;
14+ } ) ) ;
15+
16+ // Usage
17+
18+ fetch ( 'http://ietf.org/' )
19+ . then ( body => console . log ( body ) )
20+ . catch ( err => console . error ( err ) ) ;
Original file line number Diff line number Diff line change 1- # HttpRequest
2- HTTP, XMLHttpRequest, fetch
1+ # HTTP Request, XMLHttpRequest, fetch
You can’t perform that action at this time.
0 commit comments