This repository was archived by the owner on Nov 16, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ var net = require ( 'net' ) ;
2+ // Because we're building HTTP on top of raw TCP
3+
4+ var url = require ( 'url' ) ;
5+ // We need to parse URLs into their parts -- the host and port and path are most interesting.
6+
7+ module . exports = {
8+ request : function ( uri , method , body ) {
9+ var u = url . parse ( uri ) ;
10+
11+ var c = net . createConnection ( { host : u . hostname , port : u . port || 80 } ) ; // No https for us! It's complicated.
12+
13+ // The minimal HTTP/1.1 request:
14+ // GET / HTTP/1.1
15+ // Host: foo.com
16+ // <this line blank>
17+ c . write ( method + " " + u . path + " HTTP/1.1\r\n" ) ;
18+ c . write ( "Host: " + u . host + "\r\n" ) ;
19+ // We could write request headers here. Eventually we could create an interface for that.
20+ c . write ( "\r\n" ) ; // End of header -- a blank line.
21+
22+ if ( method == 'HEAD' || method == 'GET' ) {
23+ // HEAD is just like GET, only the client tells the server they don't want the data, just the metadata.
24+ // Web crawlers use it to make the check if something is fresh cheap, sometimes.
25+ c . end ( ) ;
26+ } else {
27+ c . end ( body ) ;
28+ }
29+
30+ // Just passing back c, raw, with whatever the server sends isn't a good interface but it's enough for now.
31+ return c ;
32+
33+ }
34+ }
Original file line number Diff line number Diff line change 1010 "license" : " ISC" ,
1111 "devDependencies" : {
1212 "tap" : " ^2.2.0"
13+ },
14+ "dependencies" : {
15+ "bl" : " ^1.0.0"
1316 }
1417}
Original file line number Diff line number Diff line change 1+ // tap is our test harness
12var tap = require ( 'tap' ) ;
3+
4+ // We use the node http module to create a server to use while testing
5+ var http = require ( 'http' ) ;
6+
7+ // bl = "buffer list" -- it's a stream sink, it collects everything the
8+ // stream emits as a pile of buffers, and gives it to you in one chunk
9+ // at the end. Great module.
10+ var bl = require ( 'bl' ) ;
11+
12+ // client is the library we're making, the subject under test
13+ var client = require ( './' ) ;
14+
15+ tap . test ( 'the library loads a thing' , function ( t ) {
16+ t . ok ( client ) ;
17+ t . equal ( typeof client . request , 'function' ) ;
18+ t . end ( ) ;
19+ } ) ;
20+
21+ tap . test ( 'makes a simple GET request' , function ( t ) {
22+ var server = http . createServer ( function ( req , res ) {
23+ t . pass ( 'Got request' ) ;
24+ t . equal ( req . url , '/' ) ;
25+ res . end ( 'Hello' ) ;
26+ server . close ( ) ;
27+ } ) . listen ( 0 , function ( ) { // 0 means "Hey, Operating system! Assign us any free port!"
28+ var res = client . request ( "http://localhost:" + server . address ( ) . port + "/" , 'GET' ) ;
29+ res . pipe ( bl ( function ( err , data ) {
30+ t . error ( err ) ;
31+ t . ok ( data ) ;
32+ t . end ( ) ;
33+ } ) ) ;
34+ } ) ;
35+ } ) ;
You can’t perform that action at this time.
0 commit comments