yiddish.ninja
Menu
  • About
  • Bedtime Stories
Menu

Making Code Stick: Day 6

Posted on June 16, 2015 by Greg

Getting near the end here, just three lessons left.

Lesson 11: HTTP FileServer

Obviously, we could have read the file into memory, converted it to a string, and passed it back out, but with the fs.createReadStream() method, we can just pipe the incoming stream to the outgoing stream and avoid all the intermediate messiness.

The interesting part is that my code passed despite missing an important line.

My HTTP File Server Code
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
var http = require('http');
var fs = require('fs');
 
var port = process.argv[2];
var filepath = process.argv[3];
 
var server = http.createServer( function (req,res){
  var src = fs.createReadStream(filepath)
  src.pipe(res);
})
 
server.listen(port);

Between lines 7 and 8 of the code sample above, the demo sample did something I forgot to do (and something they didn’t check for)…

res.writeHead(200, { 'content-type': 'text/plain' })

They set the response header first. Coming from the web/browser world, I’d think I’d have remembered that, but I didn’t even think about response headers. And this is an important thing to remember. Even if headers weren’t checked for here, a number of libraries look for the response code to determine how they should handle the data coming back. If you send a response code of 200, they know everything is all right, but if it’s 404, then they know there’s a problem. It’s important to send response codes when sending HTTP responses.

Lesson 12: HTTP UpperCaserer

This one has you install the through2-map library.

UpperCaserer
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var map = require('through2-map');
var http = require('http');
 
var server = http.createServer( function (req,res){
   if(req.method=="POST"){
    res.writeHead(200, { 'content-type': 'text/plain' })
    req.pipe(map(function (chunk){
      return chunk.toString().toUpperCase();
    })).pipe(res);
  }
 
});
 
server.listen(process.argv[2]);

Two things I noticed. One, they didn’t send a header this time. It doesn’t matter to the final result of the exercise if you do, but the inconsistency bothered me.

Two, I was curious about what the piping through the through2-map module was replacing, so I rewrote a simplified version of the solution that also generated output that worked.

simplified version of uppercaserer
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var http = require('http');
 
var server = http.createServer( function (req,res){
   if(req.method=="POST"){
    res.writeHead(200, { 'content-type': 'text/plain' })
    var buf = "";
    req.on('data', function (chunk){
      buf += chunk.toString().toUpperCase();
    })
    req.on('end', function(){
      res.write(buf + "\n");
      res.end();
    })
  }
});
 
server.listen(process.argv[2]);

Basically, req is implementing the Stream.readable() class, plus some goodness added by the http module. If you’d rather collect the incoming data into a single string than pipe the stream through a conversion function and right back out, you have that option. You probably wouldn’t want to… but you could.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • What is Programming?
  • Becoming a Raspberry Pi Certified Educator
  • MOD Pizza’s Cauliflower Crust
  • Keeping my resolutions
  • Pain and Python

Archives

Categories

Disclaimer

The views and opinions expressed on this site are my own and do not represent any organizations, projects. or businesses with which I am involved as an employee, member, participant, or leader.

Recent Comments

  • Ron on Lowe’s: A Case Study in Bad Customer Service
  • Greg on Viewing Someone’s Previous LinkedIn Posts
  • Abhiram Venkitela on Viewing Someone’s Previous LinkedIn Posts
  • Pikachu1902 on Sharing Your Local Minecraft Server Over the Internet
  • John MARTIN on Mags for your Canik TP40

No Warranty

I'm human, I'm imperfect, and I make mistakes. There is no guarantee of accuracy, serviceability, functionality, or applicability of anything I write or link to here. By viewing this site, you agree that you are solely responsible for all outcomes related to that act and release me from any liability.
©2019 yiddish.ninja | WordPress Theme by Superb Themes