Gwen Bell

Location: San Francisco

Find me on ssbc.

Talk to me on Decent social or via email.


Your First Node.js Server Over a Porter


December 21st 2016

I'm sitting at a cafe in Mexico City. I was annoyed with the noise level outside the window at 9 in the morning so Everett and I ran out of the apartment, caught the train going anywhere (it's 5 pesos, which, if you're tracking the peso/dollar exchange rate you know means not much if you earn in dollars). It's 32 pesos for a latte. This latte:

With the sound of the espresso machine and good music on the speakers, I can concentrate on the 5 tasks I pushed to scuttlepub this morning. Code and yoga. Those are the two things main things on my mind these days. I wake up in the morning, do my yoga practice. Then I drink espresso.

Then, I code.

The Consortium, The Yoga

A few days ago I was invited and accepted the invite to the Secure Scuttlebutt Consortium. Pretty cool, since I've been working for this project for almost a year. I remember when Everett told me about sbot. It was February 2016. He was sitting in the Oldsmobile with the barely functioning heater on. It was freezing outside. I'd just finished a shift. I was exhausted and he was thrilled. Almost as thrilled as I remember him being in Mexico in November/December 2011 when he read a piece on Bitcoin. Alright, so I end 2016 a stronger coder and stronger yoga practitioner than I started it. You want to join in the code/yoga thing? If yoga is your strong suit, skip to the code.

If code is your strong suit, here's an easy way to open up your front body, shoulders, wrists:

That's the yoga. Now, on to the code.

But wait! Let me transport you back to a year ago in that Oldsmobile with the barely functioning heating unit. It was December 2015. I wasn't working with servers. I was performing the duty of a server. I was a bartender at a bar.

The Bar

Have you ever been a bartender? Have you ever been a client at a bar?

Either way, you know what a bar is and how it works. In December last year, working behind the bar, there was a beer I thought was stellar. It was the People's Porter made a few hours away from where I stood, pulling shots and performing bartender duties. Foothills in Winston-Salem makes People's Porter, perfect for the cold winter months. At the bar I worked at you could get a pint for $3 on pint nights. Stellar deal. The warming cocoa undertones for winter nights. Tasty porter, that.

As a bartender, acting the server part of the server-client relationship in a bar I didn't have the time, energy nor urge to work with servers once I finally got done with a shift. Which often didn't end until 3 in the morning.

What's a server and a client got to do with this piece, you wonder?

A server and a client is what you have to understand to understand the beauty and ease of Node.js.

But first, back to the bar. Let's say you asked for that porter. I, when bartending, performed the duty of the server. You, performing as the client. Me, functioning as the server.

You give a set of directions to the server, the server walks over to what's called a POS (point of sale) and enters the information given by the client. The server (in this case, via the POS) indicates whether or not there's any of that beer available in the keg. If it's available, I (also acting the role of server) pull the lever that makes the porter fill up the pint glass.

I return the now-full glass to the client, you.

Requests and responses. All day, all night.

That is the behavior of the server-client relationship with NodeJS, too. There are requests, there are responses, and sometimes, there are errors.

Write Your First Node.js Server

Whether you're drinking a beer, coffee or cup of chamomile tea right now, if it's at a public place, please tip your server. Now here's how to get started with Node.js servers.

Thing 1 is you have to have Node installed on your machine. Start there.

  1. Install Node.js.
  2. Open a text editor such as Atom or if you prefer something more challenging such as Vim (I tried the former earlier this week and still prefer Vim).
  3. Type the following code into a file called bar.js using either atom or vim (or whatever text editor you use)
var http = require('http');
http.createServer(function (req, res){
  res.end('bar\n');
  }).listen(3000);

console.log('FooBar server running at http://localhost:3000/');
  1. On your command line start the server with

     λ node foo.js
    
  2. Navigate your favorite web browser (here acting as the client) to http://localhost:3000 and see your first Node.js server.

Nou know about People's Porter and how to start your own Node.js server. There are worse ways to spend an hour in December.

Experimental comment section powered by sbot -- those with sbot accounts can comment. Comments displayed with scuttlebot viewer:

Navidad 2016 ➡

⬅ A Way Out