Node.js — The basics

Digby Knight
3 min readJan 22, 2016

This is the start of a series of posts on how to get started with web development in Node.js, and will take us on a journey through a world of development, deployment, logging, hosting, content management, caching, security, CDN, payment gateways and monitoring; all in the cloud and all for free.

First, we need to get you set up with Node.js. Visit https://nodejs.org/ and download and install the latest version of Node for your environment. If you’re looking for long-term support for your application, then stick to a even numbered major version (e.g. 4.2.6 at time of writing).

At the command line, type

to check Node has been installed properly. The second command is to show the version of npm, the package manager that comes with Node. More on that later.

If all is well and good, you should see the version number you installed.

So, what next? Well, a decent text editor is important, and you can either stick with something you know, or go for an editor like Microsoft’s Visual Studio Code, which has many great features, but particularly impressive are its Node.js debugging and code assistance, as well as its increasing array of extensions. Give it a try or stick with the one you’re comfortable with.

We need to create some code now, but before we do that, we really want to make sure our source is version controlled. To make the most of some of the tools I’ll be introducing later in the series, we’ll be using Git. So, create your project folder and initialise the Git repository.

There are plenty of tutorials and books out there for learning git, so I won’t try and add to it, but make sure you understand branches, pushing and pulling, and remote repositories (more on that later too).

Okay, we now need to start creating code. The next step can all be done by hand but creating a project using npm will give you a kick-start:

This will take you through a series of steps to create your Node.js project. You can give the project a name, a version and description as well as some other attributes that will be stored in a new JSON file in the root folder of your project called package.json. This file is the core of your application — the other main file being the entry point file you chose earlier. Here’s what my package.json file looks like:

Now we need to create our main file; in this case index.js. Let’s edit it and add some code — just go with something simple for now.

Then we can run the file.

Remember to commit your changes to Git every now and again.

So, we have Node.js running. Now we want to set up a web application. For this (unless you want to write your own framework) we are going to install Express, a very popular framework with many of its own extensions (or middlewares). To install it, we need to use npm again.

This command tells npm to install express and save a reference in your package.json file. Take a look at it now.

You’ll notice that Express has been added as a dependency. The number after express is the version of the package using semver format, but for now you’ll have the latest version.

Have a look in your project folder too. You should now have a folder called node_modules, which will contain the Express package. Now is probably a good time to add that to your .gitignore file to prevent you from versioning an great many files that you don’t need to. If you lose the node_modules folder, just run “npm install” again to get them back.

Next, we need to write our first web application. Create a file called app.js and copy in the following code.

And run the following command.

Browse to http://localhost:3000 and see what happens. You should see Hello World! appear in your browser. Congratulations on your first Node web application. See the Express.js documentation for more details.

In the next tutorial, we’re going to look at adding Cloud services to your application — all for free!

--

--