Networking and Streams HTTP Core Streams
Transcript from the "HTTP Core Streams" Lesson
>> Also, HTTP servers provide streams. In this case, instead of a duplex stream that would have would sort of bundled the readable and writable side. If you have a HTTP server with a request and response, then the request object is readable, so you can pipe from it to somewhere else.
[00:00:20] And the response object is writable. So you can write to that stream, or you can pipe something into that stream. The opposite is true when you make a request, which is like a client connection with http.request, or when you use something like the request package from NPM, or similar packages.
[00:00:38] So in that case, the request is writable because you're the client and you're sending data. So you can send your post body form, upload that kinda thing. And the response is gonna be readable because it's the response from the server. So it's sort of like the flip side.
[00:00:55] Whether you're making, when you're handling a server connection or whether you're sending a request from an HTTP client. So while we go ahead and play with those a little bit, cuz I think that's a good something. We've already done that a little bit already. But as we'll make a server, and then we'll make a client and maybe in our server will pipe from a file to the response object.
[00:01:19] It's a fun thing to do. Of course, there's plenty of modules for doing things like serving up static files that you should use instead, but this is a good exercise nonetheless, okay? So we're gonna need the HTTP module, and we're also gonna need the FS module. So we do http.createserver, and pass in a function.
[00:01:41] The function is gonna take a request, it's gonna receive a request instance and a response instance. And so, the request is readable. So if we have req.method === 'Post' or input. Then, maybe we will pipe the request to standard out. And once, the request is finished we can maybe correct that end, okay?
[00:02:12] But otherwise let's read from a file and just pump it out to the response. So what files do we have? I guess we have this file cool.txt. That looks good. Maybe hello.txt. Sure. So we can do fs.createReadStream('hello.txt').pipe(res). And you can also do things like set headers and whatever probably how to do that.
[00:02:43] So do setHeader('content-type',' text/plain'), okay? So if I run this program now, I gotta listen on a port of course, okay? So now I can make this server run soon. Next, we need an http-client. We can use http.requests to do that, and request and response are flipped in terms of writable and readable.
[00:03:15] So we get the request back from http.request. And then, you have to provide it some things like maybe, we'll send a POST method first, and then, we can send a get method in a sec. So url, and then, we get back the response. So, we can print out the res.statusCode.
[00:03:41] And then, we'll pipe the response out to standard out. Now, it's a post, so we need to write some data. So, we could call that write and then call that end. But there's a nice shortcut that I mentioned in the part about writable streams where you can just provide the data like that.
[00:04:00] Just in one go, all right? So, again, we have our server, whoops, which looks like this. If we get a post, then we're just gonna dump the data to standard out. And then, we'll, once the request is finished, we'll print, okay? And otherwise, we're gonna take data from hello.txt, and we'll pipe it to the response.
[00:04:26] This is what the server does. So I'll run it. Now, we can run our client program, which sends a POST request to slash on our server. Sorry, I've actually gotta put in a real url here. 5000/. And then, we're gonna print out the status code, and we're gonna pipe everything else to standard out.
[00:04:52] We should get the message. Or maybe it's right, I think actually with the http.request. You've got to provide the host and the port separately for some reason, and it's, maybe it works like this. There we go. So that's what I expected. So it says, okay, and then if we look over here to our server, we see the message that we sent from the client program that we just wrote.
[00:05:26] So that's a post program. The other thing that our server does, is our server can respond with the file hello.txt. So we need either to modify our existing client or to make a new client program to do that. So I'm gonna move http-client.js, call it http-post-client.js. And then, we can copy that to be http-get-client.js.
[00:05:55] So now, we can make a get request. And we don't need to end the request because the request object is not a writable stream when we send a get request. So if I run the server again, whoops. And now, if I run the get client, then or maybe, I do need to end it actually, because I guess you do need to end it.
[00:06:26] That's interesting. Anyways, here I get the status code is printed out in our client program. And then, the response is pipe to standard out which is from that file hello.txt. So just shows you what's up with that. Gonna go ahead and add these to the get repo.