http://theholmesoffice.com/Ghost 0.11Fri, 09 Mar 2018 05:44:47 GMT60http://theholmesoffice.com/book-getting-mean/7e041aa8-236a-43ef-b267-77b47b532c84Mon, 13 Jan 2014 22:47:33 GMTI’m very happy to announce that my second book is now available through the Manning Early Access Program (MEAP).

I’m personally really excited by the MEAN stack. End-to-end development using only JavaScript is a tantalising prospect made very real by this stack. Each part of the stack is exceptional in what it does, and the way they all work together is mind blowing.

I find the stack to be great for rapid prototyping, taking an idea and turning it into reality. Approached in the right way you can build up an application step by step, adding the right technology layers at the right time.

Everything you need to get started with Node.js, Express, MongoDB and Angular (MEAN)

Best practices to structure your code for efficiency and reusability

This book includes a quick JavaScript recap, but some web development experience is helpful. It’s especially useful for front-end developers who want to learn more about the server-side of a web application.

Getting MEAN table of contents

And here’s the table of contents.

Part 1: Setting the baseline

1 Introducing full stack development
2 Reintroducing JavaScript

Part 2: Building a Node web application

3 Creating and setting up a MEAN project
4 Developing a static site with Node.js and Express
5 Building a data model with MongoDB and Mongoose
6 Writing an API: Exposing your MongoDB database to the application
7 Using your API from inside your application
8 Logging in users with Facebook and Twitter

Want 37% off the price?

What’s the MEAP anyway?

The MEAP is Manning’s Early Access Program. This means that you get the book chapter by chapter as it is written, before getting a copy of the final version. You can also join the online forum and give give feedback and comments that can change the final book.

]]>http://theholmesoffice.com/book-mongoose-for-application-development/ab19b719-8a33-40a1-af76-427d746ac357Wed, 18 Sep 2013 21:07:30 GMTI’m very happy to announce that my book “Mongoose for Application Development” has been released!

The book was commissioned by Packt Publishing, and was released on 26th August 2013.

Mongoose for Application Development is a practical, hands-on guide that takes you from installing the technology stack through the steps of developing a web application. It covers the key features of Mongoose and how to use them to rapidly develop a Node.js and MongoDB application.

A quick overview:

Rapid application development with Mongoose on the Node.js stack

Use Mongoose to give structure and manageability to MongoDB data

Practical exampless on how to use Mongoose for CRUD operations

Provides a number of helpful tips and takes away the complexity of everyday MongoDB operations

Walks you through building a project management application using Mongoose, Node.js, MongoDB, and Express

This book is ideal for people who want to develop applications on the Node.js stack quickly and efficiently. Prior knowledge of the stack is not essential as the book briefly covers the installation of the core components and builds all aspects of the example application. The focus of the book is on what Mongoose adds to you applications, so experienced Node.js developers will also benefit.

Free review copies

]]>http://theholmesoffice.com/mongoose-connection-best-practice/0c0dba7d-cbda-4027-9ff4-dee888dcb2e3Mon, 26 Aug 2013 06:28:17 GMTThere is often quite a lot of confusion about how best to set up a database connection with Mongoose. So I thought I’d clear it up!

There are two ways of establishing a Mongoose connection, using the default connection or a named connection. In this article we’ll be looking at using the default connection.

Let’s start with a list of things we want to achieve:

Open the connection when the app starts

Monitor the connection events

Close the connection when the app process terminates

Define a schema and build a model that we can use in the app

Defining the Node.js app

Let’s define a really simple skeleton Node.js app, using the following file structure.

app.js
pages.js
model/
- db.js
- team.js

app.js will be the starting point of the application, creating the server and tying everything together. pages.js will contain a rudimentary controller to interact with Mongoose and display output to a browser window model/db.js will hold the database connection and event listeners model/team.js will hold a Mongoose schema definition

Starting with app.js, we need to require the HTTP module, the db file and the pages file. We’ll also create a server that listens to the localhost port of 8888, serving an index page that we will define later in pages.js.

Managing the Mongoose connection

Our model/db.js file is where we’ll hold the database connection information and event handlers. We’ll also import our schemas & models into here so that the application has access to them. The comments in the code should make it pretty obvious what’s going on here.

Using the Mongoose connection

Finally, we want to do something with the connection. So in pages.js we want the following code. What we’re going to do is require Mongoose, bring the Team model in, create a new team and output it to the browser window.

Running the test page

So there we go. As you can see it’s pretty straightforward to create a default Mongoose connection and use it in your application. You can test the disconnection script and event handler by terminating your Node process. In the terminal window running the Node app just hit Ctrl + C to kill the process.

Download the code

]]>http://theholmesoffice.com/node-js-express-and-mongoose/36d37ed0-95c5-456f-a76d-ec0b74240dd0Fri, 01 Feb 2013 10:43:01 GMTPreviously we have used Node.js with Mongoose, and Express with hard coded data. Now it is time to bring it all together using Node.js, Express, MongoDB and Mongoose.

Start with the Express project

I created a new copy of the Express project from the previous tutorial, source of which you can find on GitHub.

This has got our simple route and single page of info, but the data list for the teams is hard-coded as a JSON object in the ‘routes/index.js’ file.

Install Connect and Mongoose

The easiest way to do this is to use the package.json file in the root of the project and npm. This method allows you to install all of the modules you need at once, or you can add some in later, like we are doing now. This is a useful habit to get in to.

Getting data from the database

In the ‘model’ folder create a file teams.js. We will use this to search the database for teams with a given ‘GroupName’ and return a JSON response. If you had other ‘team’ related queries they could be placed in here – e.g. all data about a given team.

Our file will need an export function that:

Takes a ‘GroupName’ value as a parameter

Takes a callback function as a second parameter

Searches the MongoDB database for teams with the provided ‘GroupName’

Send the database output to the callback function.

We created a Team.find() function last time round, so by taking that, bringing in the Team model as a local variable and requiring Mongoose, teams.js should look something like this:

Establish the Mongoose connection

Mongoose is designed to have a reusable connection created on application startup. It also makes sense to only define the schemas once. So as we start our app by calling app.js this is where we need to require db.js. Let’s add it in to the require section at the top of the file, just after requiring Express.

Now that we have the building blocks of Node.js, Express, MongoDB and Mongoose in place, next time we’ll start building a web app we can actually use in the real world.

]]>http://theholmesoffice.com/express-js-framework-node-js/7e23cfac-70f7-41e3-b550-5ed5dd074746Thu, 17 Jan 2013 23:58:39 GMTUp to this point in the Knowing Node series we have used node.js to create a server, built a rudimentary MVC framework, and served a webpage pulling data from a MongoDB database. We used Mongoose to make working with the database easier, and now we’re going to look at a way of making the basic server and framework setup easier. Say hello to Express!

What is Express?

Express is a popular web application framework for node. The first thing you’ll enjoy is that it simplifies the web server setup – including routing for static files – and creates a directory structure as a start point for organising your code.

We’re going to use it as we’re looking at building a web application, whether that’s a one page app or a multipage site isn’t important right now.

Installing Express

First of all we need to install Express globally. So in terminal:

sudo npm install -g express

Creating a project

Now that we’ve got Express installed globally, we let’s use it to create a new project. Here’s we create a new folder for our Express project, and install Express and it’s dependencies.

Start the project and view it in a browser

In terminal:

node app

This should display a confirmation in terminal “Express server listening on port 3000″. 3000 is the default port used by Express, so head over to localhost:3000 in your browser and you should see something like this:

Nice. That was pretty easy right?

If you look at the page you may notice that it’s not using the default browser font, meaning there must be a stylesheet included.

Take a look back into the terminal that you use to start the process and you should see confirmation that it has delivered the URL ‘/’ and the stylesheet ‘/stylesheets/style.css’.

So there we go, a few terminal commands and you’ve got a node.js webserver and framework in place. More excitingly we’ve got that basis for a web-app just waiting for us!

]]>http://theholmesoffice.com/knowing-node-source-code-files-on-github/56839288-e3dd-4ea2-bc33-c3be860f0e34Mon, 12 Nov 2012 23:33:39 GMTAll of the source files for my Knowing Node series of tutorials on Node.js are now available on GitHub.

]]>http://theholmesoffice.com/mongoose-and-node-js-tutorial/8a167542-934e-4921-82f7-9d3e0a78c45fWed, 31 Oct 2012 08:34:23 GMTThe aim of this tutorial is to take away some of the hassle when using MongoDB with Node.js. Last time out we used the Node.js native MongoDB driver to get data out of a MongoDB database. This worked well, and retrieved our data. But it did seem like an awful lot of code and callbacks for such a simple operation.

Here’s an easier way.

Introducing Mongoose!

In their own words Mongoose provides – “elegant mongodb object modeling for node.js” and “solves common problems for real-world applications”. Essentially, for our purposes here, it exposes only what you need to see and does everything else behind the scenes.

Installing Mongoose

cd into the root directory of your site – I’ve made a copy of my “testpage-site” folder called “testpage-mongoose” – and enter:

sudo npm install mongoose

Now we’re ready to go!

Taking our code from last time as a start point, we are going to need to look at our controller file, and our model file.

Changing the model file

Most of our work here will be in the ‘model’ file, as we are dealing directly with the database, so let’s start there. For this I have created a new file ‘mongoose-data.js’ using ‘mongo-data.js’ from the previous tutorial as a template.

Connecting to MongoDB using Mongoose

This will be where Mongoose is going to manage the connection to our MongoDB database, so let’s start off with this code snippet:

Note that we now require ‘mongoose’ instead of ‘mongodb’. We have also very easily created a connection to our database ‘euro2012′ on the server ‘localhost’. Finally, we have created a default error handler to catch any connection errors.

Using Node.js and Mongoose to query MongoDB

Let’s dive straight into the meat of the code and see what is happening in our ‘export’ function. The export function itself expects two parameters, a variable ‘gname’ that we will search for in the database, and a callback function that we’ll execute when the database query is complete.

First, we open the database in the db.once wrapper, and send the rest of the code through as a callback to be executed once the database connection is open.

Setting a schema and building a model

Mongoose requires a schema to be set for the data. This enables Mongoose to then build a model framework for the information we will be dealing with. Ours is pretty simple here, using just a couple of strings – see the Mongoose docs for more info on schemas.

And here’s the simple one liner to build a model ‘Team’ from the schema:

var Team = db.model('Team', teamSchema);

Using Mongoose “find” to query MongoDB

The “find” method of Mongoose is very similar to that of the native driver, and is happy to accept the same filter object as an argument. You simply apply the find method to the model you’ve just created, sending the filter object/search query as the first parameter, and a callback function as the second. This callback function has two possible parameters, an error object and the returned data following the query.

The Mongoose ‘find’ method automatically returns an array of JSON objects, reducing the amount of coding effort you have to go to in order to make the data more usable. Our console.log from the above snippet returns something along the lines of:

Secondly, we have changed the way the main ‘get’ function works very slightly by providing the name of the group of teams we want. In this case “D”. This means that we will search the database for this and return the relevant teams (this is what we did in the model at the start of this post).

After that, looping through the list and sending the output to the template is pretty much the same as before.

That’s it!

Now we have Mongoose acting as an interface to our MongoDB database, taking much of the coding pain away and simplifying the process. Coming up next we’ll make life even easier for ourselves by using the Express framework.

]]>http://theholmesoffice.com/node-js-fundamentals-how-to-upgrade-the-node-js-version/ac569103-2997-432e-aa88-b1f90b9549dcWed, 10 Oct 2012 07:00:08 GMTNode.js is being actively developed, and the latest stable version frequently changes. From time to time you will find a module that doesn’t work with your current version of Node and says that you need to upgrade.

4: Upgrade to a later version (this step can take a while) You can specify a particular version like so:

sudo n 0.8.11

Or you can just tell the manager to install the latest stable version like so:

sudo n stable

5: Check the running version of Node to verify that it has worked:

$node -v v0.8.11

If the version doesn’t number output in step 5 isn’t what you were expecting, you may need to reboot.

]]>http://theholmesoffice.com/how-to-ge-data-from-mongodb-into-node-js/b2435693-73ed-48df-b51e-3295d52fcceeFri, 28 Sep 2012 06:50:30 GMTSo you’ve got a Node.js website, and created a MongoDB database - the big question – how do you connect the two? When we installed MongoDB we also installed the native MongoDB drivers for Node.js, so that’s a good start!

In the MVC approach we will need to look at the ‘model’ code and the ‘controller’ code.

Make your code ‘non-blocking’

We will start off by looking at the controller. In a traditional stack you say:

I’m going to go and get the data from the database

When I’ve got it I will do this with it

This is no good for Node.js as it is a ‘blocking’ operation – nothing else can happen until you have the data back. This is essentially what we did in the MVC framework with this bit of code in home.js.

This is very similar to our *home.js *file, just a few important differences.

Firstly, we ‘require’ a different model file, mongo-data.js that we will look at shortly.

Secondly, rather than pulling the data and doing something with it, we are using a callback function to say “do this with the data when you’ve got it”. What we are doing with the data is exactly the same as before. Note that we are sending the string “D” through as a variable; this is the string we will be searching for in the database.

Thirdly, through the callback function we are also sending instructions for what to do if an error occurs getting the data.

Using Node.js to query the MongoDB data

Before we look at the code, let’s think about what we’re looking to achieve:

Connect to the MongoDB database

Have a function that is expecting a string variable and a callback

Search the database for the variable (a specific key of ‘GroupName’)

Create a JSON object if successful

Catch the error if not successful

Pass the JSON object or the error to the provided callback function

Close the database connection

Easy? Not as hard as you might think. So here’s the code for the *mongo-data.js *file:

The first five lines are all about setting up the MondoDB server and connecting to our database. You can learn more about that on the MongoDB Native Driver GitHub page.

Then we have a function onErr that we will call if we catch an error at any point.

Now comes the fun part! The MongoDB Native driver comes with a number of non-blocking functions built in. We are using three, to:

Open our database (db.open)

Find the ‘collection’ we specify (db.collection)

Find the documents in that collection that match the query we specify (collection.find().toArray)

These all run as nested callbacks, which is where the code can get tricky to follow. To help with this I recommend adding a comment at the end of each callback function, like you can see in the code.

Counter-intuitively, the collection.find method doesn’t actually execute the query, that’s done by the toArray method. So that’s where we pass our next callback function. This part is where we take the docs returned by the database and use them to create a JSON object in the format we want.

]]>Having a node.js website without a database in the backend is going to be pretty limiting! So let’s address that.

There are a whole bunch of databases that we could use, but we’re going to go with a popular choice for Node.js development, MongoDB. In their

]]>http://theholmesoffice.com/how-to-install-mongodb-on-ubuntu-for-node-js/0ae2a834-5c86-4e29-8a13-4dd8b03249e9Mon, 16 Jul 2012 06:30:33 GMTHaving a node.js website without a database in the backend is going to be pretty limiting! So let’s address that.

There are a whole bunch of databases that we could use, but we’re going to go with a popular choice for Node.js development, MongoDB. In their own words:

Installing MongoDB on Ubuntu

If you Google how to do this you might get put off, as you’ll find a load of pages with different workarounds to get MongoDB installed correctly, Q&A threads on fixing install errors etc. Fortunately this has all been fixed, and it’s now super easy to install MongoDB on Ubuntu.

Testing your MongoDB install

You want to test to make sure that it’s installed correctly? Fair enough. Let’s start it up as an Upstart service (more on Upstart at another time).

Start the MongoDB service

In terminal enter:

sudo service mongodb start

You should now see a confirmation along the lines of mongodb start/running, process 15251

Using the MongoDB command line shell

MongoDB comes with a command line shell. We’ll use this to verify everything is running correctly.

To enter the MongoDB shell, in terminal simply enter:

mongo

This should tell you the shell version and connect you to the ‘test’ database that ships with Mongo. You should see something like this:

The “>” indicator at the start of a new line shows that you are now working in the MongoDB shell, not the Ubuntu command line.

Listing all databases

While we’re here, let’s see what MongoDB database have been created during installation.

In the MongoDB shell, enter this command to list all databases:

show dbs

You should then see something like this screenshot, showing two databases and their sizes.

Exiting the MongoDB shell

Press Ctrl+D to exit the MongoDB shell and return to the terminal command line interface.

How to install the native MongoDB drivers for Node.js

So having got MongoDB installed, we want to make sure that our Node.js applications will be able to use it. For this we need to install the native driver. Note: there are actually loads of middleware modules to simplify and extend the possibilities which we’ll cover at another time. For now we’ll stick to the basics so that we’ve got a solid foundation to build upon.

To install the native MongoDB drivers we’ll use NPM, so head to terminal and enter:

npm install mongodb --mongodb:native

This will run through the installation process, downloading everything we need to get MongoDB and Node.js talking to each other!

]]>http://theholmesoffice.com/getting-ready-for-scalability-creating-an-mvc-framework-for-our-node-js-page/f51e72aa-5c34-43b8-8137-f2228c95b35fMon, 09 Jul 2012 06:44:20 GMTHaving got our simple webpage working in Node.js, it’s now time to think bigger. But we can’t very well create an entire website in just one file, so let’s look at how to create a framework upon which we can base our future development.

If you’ve spent much time as a developer you’ll know that the key to maintainability is separation of concerns across your projects. If you’ve been developing a long time, since before such architectures were really discussed, then you probably learned the hard way. Anyhow, what we mean, and what we want to achieve is to separate out our data, logic and presentation.

MVC – The Model-View-Controller approach

A very popular approach for web sites & web apps – and the one we’ll be working with – is MVC, which stands for model, view, controller. This makes distinctions between the model (data), view (presentation) and controllers (logic).

The only new thing there, is that rather than outputting anything directly here, we are calling in a ‘router’ module and sending it the request & response object. This makes sense if you look back at the steps the application needs to take: server sends request to router.

Building the router

In the same folder as the server.js file, create a new file called router.js and save it with the following content:

Importing the URL and FileSystem (fs) modules that come as part of your node.js install

Exporting a function called “get” – this allows our server file to use this function, passing the request and response objects through

Getting the path of the URL request

Testing to see whether the request is for a CSS file and loading it in if so. There are a number of different ways of handling static files, but this method is fine for what we’re doing here

Sending the request to the correct controller, if it’s not for a CSS file

At this point in our development, it’s number 5 we are interested in. We are checking for two URL paths ‘/’ and ‘/home’ which will route to a controller called ‘home’. We then send any other URL requests to a controller called ’404′. Let’s take a look at these controllers.

Building the controllers

Create a folder in the root of your site, called ‘controllers’. Inside this folder create a file called ‘home.js’ with the following content:

Right at the top of this file you can see that we are including the rest of our MVC structure the model and the view. Referring back to the diagram we remember that the controller should do two main things:

The controller (normally) performs some operations using the response from the model

The controller sends a response to the view

Performing operations on model data

Our task here is quite simple: get a data object (teamlist) and create a HTML string putting the items into an unordered list. In the next step we’ll see how we create this data.

Sending a response to the view

We will use a view (think template) called template-main, which we will create in a few minutes. Note that through the template.build function that we are only sending three content items; within the controller there is no reference to how this content will be displayed. In this example you will notice that we are sending through the HTML for the <ul> list, there are arguments against having even this level of markup in a controller, but we’re going for a simple example here so let’s keep it in!

Getting data from the model

In a future post we’ll see how we can extend this demo by getting the data out of a database, but for the sake of remaining focused on the point right now we’ll fake it by creating and returning a JSON object.

So, create a folder in the root of your site called ‘model’. Inside this folder create a new file called ‘test-data.js’ with the following content:

Through the export function we are exposing the function thelist() which itself is returning a JSON object. This is how – for now – we will return a data object to the teamlist in our controller.

Using a view template to create the HTML page

Now that we have our controller pulling in some data from the model and doing something with it, we want to show the outcome in a webpage. We’ve seen that we’re going to send the data to a build function in a view template, so let’s create that now.

Create a folder in the root of your site called ‘views’. Inside this folder create a new file called ‘template-main.js’ with the following content:

Here we are directly exposing the build function, which takes three parameters: title, pagetitle and content.

The first thing we are doing here is joining an array of strings, placing a new line ‘n’ between them. This creates one long string of HTML that will render on separate lines leaving the source code more readable. This string is the basis of our template, and contains a number of placeholders for the content items {title}, {pagetitle} and {content}.

Naturally then, the second step here is to replace these placeholders with the actual content sent through via the function arguments. The three replace functions are doing exactly that, running a very simple regular expression to globally change all occurrences found in the string. This means that you can have {title} in several places in your template, and each will get replaced. Note that we are also chaining the join and replace commands; the separate lines are for readability. Seeing as it works when laid out like this there is little sense in putting them all on one line and making the script harder to read.

Using a static CSS file

You may have noticed that in the HTML view template that we are referencing a static CSS file; remember in the router how we created a statement checking for the .css file extension? Now is the time to implement it.

In the root of your site create a folder called ‘assets’. Inside this folder create a text file called ‘style.css’ with the following content:

* {font-family:arial, sans-serif;}

This isn’t the most complex CSS file in the world, but the change in font is enough for us to know that it is working!

Creating a 404 page

The final piece of our site is to create our 404 page. We already have a suitable template – our template-main view – so we’ll re-use that. Our router is also already set to send people to a 404 if it doesn’t recognise the URL path requested. So all that remains for us to do is to create the controller.

In your ‘controllers’ folder create a new file called ’404.js’ with the following content:

You should be able to see what we’re doing here. Using the same template - views/template-main - we send it three items of content. The only difference this time is that we have changed the status code of the response – in the res.writeHead function – from 200 (Ok) to 404 (not found).

Let’s run the site!

That’s all the pieces of our MVC framework in place, so let’s try out our site. In terminal cd into the root folder of your site and run it with the command:

node server.js

Now head to 127.0.0.1:8899 in browser and you should see something like this:

Let’s test our other path from the router, by navigating to 127.0.0.1:8899/home

Finally, let’s test our 404 page, by going to a URL that we haven’t added to the router, for example 127.0.0.1:8899/test

Creating the webserver

The first thing to remember – and a big change to get used to if you’re coming over from a Microsoft IIS stack, or an Apache based stack – is that with Node.js there is no webserver until you create it. And that’s what our first two lines are doing.

Calling the HTTP module

Starting at the top, the very first thing we do is load in the pre-prepared HTTP module that ships as part of node. You’ll see this type of syntax a lot, as Node.js programming uses a lot of modules. As a best practice it’s a good idea to set the variable name to match the module name.

var http = require('http');

Create the server

Stripping out the callback function to make it a little more clear, we can look at the createServer method of HTTP like this:

http.createServer(function(req, res){ }).listen(8888, '127.0.0.1');

What we are doing here is creating a HTTP server to listen to requests (req) made to port 8888 on the IP address 127.0.0.1, and also send response objects (res) back to the same IP and port. The IP address 127.0.0.1 is common across many OS’s as the ‘localhost’ server.

We also output a message to the console to show that we’ve reached the end of the script and that the server is running:

console.log('Server running at http://127.0.0.1:8888');

But what about the middle bit?!

Okay, so in a nutshell what we’re doing here is responding to any request in exactly the same way. Let’s break that response down.

res.writeHead()

The first part of the response object is another item that you’re probably not used to really thinking about if you’re coming from a traditional stack where the web-server does all this for you. We need to send the HTTP headers. Here we’re just sending the HTTP status code and content type. So to say that the request was successful, and that we’re sending back HTML content we type:

res.writeHead(200, {'Content-Type': 'text/html'});

res.write()

Next we want to send some content back to the response object. Here we’re doing it all as one long string:

Steps to install node.js on Ubuntu 12.04 using terminal

]]>http://theholmesoffice.com/installing-node-js-on-ubuntu-for-the-first-time/c9815e55-75fe-4b20-8d9b-d2491fd2f370Wed, 20 Jun 2012 06:04:13 GMTI have previously installed node.js using a third party build script but I think it’s better to do the basics piece by piece to more fully understand the whole stack.

To be honest, it’s pretty simple.

Steps to install node.js on Ubuntu 12.04 using terminal

We’ll be using the Ubuntu “Advanced Packaging Tool” (APT) to manage the installation. All code samples below should be run in a terminal window.

1: Make sure APT is up to date

In order to get the latest versions of the packages we’re going to install we need to update the APT Index:

sudo apt-get update

2: Check for dependencies

Node needs a couple of things to run properly, so let’s start off checking if they’re already installed, and installing them if necessary.

2.1: Check for a C compiler:

cc --version

If you have one installed you’ll get an output something like this:

If you don’t see something similar, install one (yes, that is meant to be a ‘g’ below!):

sudo apt-get install g++

2.2: Check Python is installed:

python

If Python is installed this will drop you into a Python command line like this (press Ctrl+D to drop back to the terminal prompt):

If you don’t see something like that, install Python:

sudo apt-get install python-software-properties

3: Install Node.js

Now to install Node.js. Fortunately this is also available through APT making life pretty easy for us:

sudo apt-get install nodejs

That’s it.

No really. See, I wasn’t kidding when I said it was pretty simple!

Next up, we’ll be testing the node.js install through a few simple terminal commands, before building a quick webpage we can view in the browser.

You can configure it to install the database of your choice at the same time

Using the ShellScript to install Node.js and MongoDB

1: Create the configuration

Using the radio buttons and checkboxes select which Ubuntu Package Manager (UPM) and database you want to use, and whether you want to install the Cloud9 IDE. As you make changes you’ll notice the text to the right of the buttons changing reflecting your choices.

I have chosen the default UPM apt-get, MongoDB and also to install Cloud9.

2: Create the ShellScript file

You can use a text editor to create the file, or use the code provided to do it for you. Open a terminal window and paste the following:

cd ~/ nano -w node.sh

This makes sure you’re in your home directory and creates a file called node.sh, opening it in Nano. Nano is a text editor that runs in the terminal.

Paste the script that you configured into the text editor. It should look something like this:

Press Ctrl+X followed by Y to exit Nano and save the changes.

3: Run the ShellScript file

Paste this into terminal to run as administrator – you will be asked for your password.

chmod +x node.sh && sudo ./node.sh

When you enter your password the script will run, downloading and installing Node.js and anything else you selected.

For me this took about 5 minutes, but it will depend on the options you selected, and your internet connection speed. When everything is finished you’ll be presented with your normal terminal prompt. Mine looked like this:

All done, Node.js is installed!

A quick reboot, and there you have it! Node.js, NPM, MongoDB and Cloud9 IDE all installed in a few minutes. I told you it was pretty easy!