Alt.Tekpub – Installing and Setting Up Node
Installing NodeJS is pretty simple these days. Understanding what's going on takes just a bit more effort. My goal today is to get NodeJS installed and then lay in the Express framework on top of it (think Sinatra for Node).
The first part is easy: head over to NodeJS.org and download your installer flavor. If you're on a Mac you could use Homebrew but I find the installer works great. It also installs the Node Package Manager (NPM) which you can think of as Rubygems (or Nuget if you're a .NETter).
The bundling of NPM is a big deal :). It used to be that Node and NPM might get out of sync, causing all kinds of headaches – nowadays they tend to be a bit more compatible.
The first package we want to install is Express. This is a Sinatra-style, bare-boned web framework that will help us keep our routes clean and give us a tad bit of structure. If we want more – there's Railway but I don't need that just yet.
So, to install Express I simply write:
npm install -g express
A couple of important things:
Make sure that you use "-g" here as it will install the Express executable in Node's global modules. We'll need that for spinning up our site. Next – if you're on Windows you'll get all kinds of fail messages if you don't run as Administrator.
Now, we simply need to go to our project directory and install an Express site into it:
cd ~/projectsexpress hello_node
You should have seen a few lines of code go by. If you didn't and received an error, it's likely you forgot the -g flag when you installed Express.
Let's jump into our new directory and install all of the modules we'll need to work with Express. These aren't installed by default – in the same way that < Rails 3.1 didn't install the gems for your Rails site by default – so we need to tell npm to drop em in:
cd hello_nodenpm install -d
You should see the installation going by. Once finished, let's run it!
Now, open a browser and head to http://localhost:3000. Whoop!