Header Parser (FCC Speedrun Project #4)

Abigail (agathalynn)
Chingu FCC Speedrun
3 min readApr 22, 2017

This project allowed me to review many of the things I did yesterday with the Timestamp Microservice Project. Setting up Express was easy (although I did refer back to the tutorial for instructions on installation), and here I didn’t even need an HTML “landing page”. The entire project consisted of 3 files — a “package.json” file, my main “app.js” file, and a third JavaScript file to store a header-parsing function.

Reflections

Before jumping into this project, I did spend a little bit of time exploring the HTTP request header, and I devoted a bit of time to figuring out how to pull some of my JavaScript code in from a separate file.

Getting Info from the Header:

I started out by doing a bit of reading online about HTTP request headers and digging around in Express’s “request” object. Before I jumped into the official documentation, I wanted to see what I could find for myself. I set up Express, and told it to log requests to the console:

This part was pretty…

And then waded through what seemed like an interminable amount of this:

…not so pretty.

If you squint a bit, you can see some of the header information. I was able to find the “user-agent” info — which contains information about the user’s system and software — and the language preferences. (I apparently prefer American English, followed by English in general, and German after that.)

The IP address remained elusive. Maybe it was hiding inside an object or something, and wasn’t printed to the console at all? I’m not sure.

In any case, once I’d taken a peek under the hood, I went ahead and solved the problem the “right” way: by finding and using Express’s built-in methods.

Modularization:

In my write-up on the Timestamp Microservice API project, I noted that it would be nice to get some of my “helper” code out of the main file. In this case, I decided to let my app.js file handle just GETting and sending information, and to store my header-parsing code in a separate file.

This turned out to be incredibly simple to do. In my headerparser.js file, I wrote my function, and then exported it:

The yellow dot is just JSHint complaining about arrow functions.

And then I used “require” to import my module into the app.js file:

Imported!

In this case, I’m exporting and importing just the function. If I had multiple things that I wanted to import and export, I could do that as well — I’d just import/export an object that contained the functions that I intended to use.

Closing thoughts

This was a fairly quick project — It took me part of a morning — but it’s definitely helping me to feel more comfortable working with Express. I’m looking forward to tackling some of the more complicated back-end projects soon! First, though, I need to take some time to learn about databases. In the meantime, I’m going to jump back to the front-end projects for a couple of days.

(No live demo. Code HERE.)

Next up: Pomodoro Clock

--

--