The what, the why, and the when

Robert Higdon
4 min readJun 5, 2015

--

I recently started a job as a front-end focused developer at a major corporation here in Seattle. The bar is set high here — higher than I’ve ever had to work toward. In this line of work, there is a daunting amount of knowledge to acquire. I’ve started to accept that I just can’t know everything. I need to focus. My memory isn’t what it used to be; that is okay.

I’ve spent countless hours reading through documentation around technology. It’s a real passion of mine. I love my work. The thing is though, I won’t retain new things unless I’m actively working on them.

This brings me to the why of this blog. I’ve quickly realized that I need to up my chops on Node.Js. It’s an open source cross-operating system javascript platform used for building applications. Started in 2009, it has quickly come to dominate the field. Now is the time to get on board.

What about the when? How about right now?

Creating your first Node server (and working with Terminal)

Open up terminal.
Keyboard’ish shortcut: Command + Space -> Start typing “Terminal”

Find your way to your home directory if you aren’t already there.

$ cd ~

Check if you have node installed (if not, pick it up at http://nodejs.org)

$ node --version

If so, you should be able to run node via your terminal directly.

$ node
> console.log(‘Hello World’);
Hello World

Make a directory for this project.

$ mkdir node-training
$ cd node-training

Create a file called server.js in the root if your project

$ touch server.js

Open this file in your favorite code editor (mine is Sublime Text). Fill it with the following:

var http = require(‘http’);http.createServer(function(request, response) {
response.writeHead(200, {‘Content-Type’: ‘text/plain’});
response.write(‘Hello World’);
response.end();
}).listen(8080);

Run your server

$ node server.js

Open your browser to http://localhost:8080. Alternatively, you can open up the server from your terminal with:

$ curl localhost:8080

To shut the server down, you can use CTRL + C.

This is nice and quick. But it’s not super helpful. You can’t really DO anything with this sort of server. Node isn’t going to hold your hand for you. What if you just want to get a quick web framework up without jumping through the hoops? For a lot of people, the answer is Express.

Starting a quick static file server using Express

To install express, we are going to use Node Package Manger (or NPM for short). To make sure you’ve got the very latest version of NPM, you can run this:

$ sudo npm install npm -g

Let’s create a file that will start the express server along with the directory that will house the files that we want visible.

$ touch fileServer.js
$ mkdir public

Create a dummy text file in your public folder so that we can make sure the server is working.

$ nano public/test.txt
Just some dummy text.

CTRL + O and then Return to save.
CTRL + X to close your document.

We will create a basic package.json for NPM to build off of. This file sets up information about the project including basic dependencies for the modules (like express) that you will be using. Modules are encapsulated chunks of code that can expose functions for you to use.

$ npm init

This will ask you a series of questions that help define the structure of the document. Feel free to fill them out however you want. It’s easy to edit later in a code editor.

Once that’s finished, we can install express via NPM.

$ npm install express --save

The save option on the end of the line automatically saves express as a dependency in your package.json

Open up fileServer.js in your editor .

var express = require('express'),
app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8080);

The top line is setting express as a variable and requiring it in as a node module. The second “app” variable is what runs it.

We are setting the app to use the /public folder as a static file server and then hooking it on to port 8080.

Another way to write this would be to expressly state the route we want to use.

app.use('/', express.static(__dirname + '/public'));

This route would make your file visible at http://localhost:8080/test.txt

app.use('/public', express.static(__dirname + '/public'));

This route would make your file visible at http://localhost:8080/public/test.txt

Wrapping up

This is a super basic start to what I hope will become a learning series for both myself and anyone willing to sit through it. Thanks for reading.

--

--

No responses yet