Installing NodeJS on Linux EC2 Instance and Viewing from a Web Browser

Web Development

I was actually pretty disappointed when I noticed that there aren’t really any good tutorials for how to install and run Node on a Linux machine.  So I thought that I would help people out here by writing something for all you facing this issue right now.  So here we go.

Prerequisites

What do you need before you can do this?

1) Some form of an SSH client, I’m using OpenSSH for Windows.  If you’re using a Mac there is already one by default.

2) ip Address of the remote Linux Instance.  I’m using an Amazon EC2 instance running Linux.  So I get my ip address from the EC2 Management Console.

3) Your .pem file for the Linux instance.

Let’s Begin

1) SSH onto your Linux instance.

2) Run the following commands

sudo mkdir node 
cd node 
sudo wget http://nodejs.org/dist/v0.6.6/node-v0.6.6.tar.gz 
sudo tar -xzf node-v0.6.6.tar.gz

3) Now you need to install the Development Tools needed to compile Node.

yum groupinstall "Development Tools"

Now install openssl-devel:

yum install openssl-devel

4) Now cd into the node folder you created ‘/node/node-vx.x.x’ and run these commands to configure and compile node.

sudo ./configure
sudo make
sudo make install

Now there’s a chance that when you uploaded your folder containing your node server and website to your Linux instance, that the folder was set to only allow super user access.  If that’s the case, you’ll see an error saying that access to your node file is denied.  Actually you won’t even be able to cd into the folder.  If that happens, simply enter in this command.

chmod -R 777 dir_name/

5) You should now be able to run node and have access to the folder and files necessary to run your app.  Navigate to the directory that contains your node file, and run this command.

node nodefile.js

You should now have your server up and running.  The only thing though is that the server can only be reached on the local machine.  What we’re going to do now is make it so that you can reach your nodejs server from anywhere.  This gets a tad bit more complicated.

Allow Remote Access From Browser to Web Server

So here’s what we need to do:

1) Open the right ports using the Amazon EC2 Management Console.  To do this, go to your EC2 Management Console, click on Security Groups, and select the Security Group you’re using for your EC2 instance.  Now you need to add a rule.  Open up port 80 (http) to allow connections from all ip addresses.

1) Good.  Now, you’re app is most likely listening on port 8080, right?  What we now need to do is set up port forwarding, from port 80 to 8080.  If you’re running on another port, then simply make a note of that in the following commands asking for the port to route to.  So first of all, run this command to see if you have port forwarding enabled.

cat /proc/sys/net/ipv4/ip_forward

2) It will most likely return 0.  If it does, that means that port forwarding is not enabled.  To enable it, enter the following command.

sudo nano /etc/sysctl.conf

If you want to you can use vim to edit this file, but that’s up to you.

3) Now change this line from

net.ipv4.ip_forward = 0

to

net.ipv4.ip_forward = 1

4) Now we need to enable these changes.  Enter this command to do that.

sudo sysctl -p /etc/sysctl.conf

5) Now check and make sure that port forwarding is enabled.

cat /proc/sys/net/ipv4/ip_forward

This should now return a 1.

6) Now lets set up forwarding from port 80 to port 8080.  Enter in the following command.

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

7) Next, let’s open the Linux firewall to allow connections on port 80:

sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

8) Now try and enter the public ip address of your EC2 instance in your web browser, and you should see that your server is up and running!

Using jQuery Datatables with NodeJS and MongoDb using mongoose-datatables

Web Development

So what exactly is this long name all about?  Well let’s just dive right into it. jQuery Datatable is an awesome tool to use as a table for your website.  It offers some nice features right off the bat such as sorting, and searching mechanisms.  So using it can save you a lot of development time. I realized rather quickly though when implementing this tool in my website that using the jQuery datatable with Node and MongoDb was a little difficult, mainly because of the lack of documentation.  That’s why I decided to write a blog post about how to create a datatable with NodeJS as the server, and MongoDb as the back end database.  Keep in mind, there are a lot of details that I will not go into that have to do with NodeJS.  So if you’re unfamiliar with Node you may want to look into it first.   So here we go! So we’re going to have four files.

  • index.html – The web page itself
  • mongo.js – This will handle all our database manipulations using mongoose
  • node.js – This will be where we write our Node server
  • main.js – This is where we handle all the client side javascript.

The example that I have here is using a document that I created entitled reservations with four fields.  The Json looks like this.

reservation : {
   name : String,
   startDate : String,
   endDate : String,
   confirmationCode : String
}

The first thing you need to do is create a table with the columns already specified.  This is an example of what you can do.  Keep in mind though that this table will not be too pretty, I’m not showing any fancy design with this.

index.html

<!--We need these two js files included. You need jquery 1.7.2 because for some reason the newer versions have issues with datatables. The second will be the js file for the datatable itself.-->
<script type="text/javascript" src="path/to/1.7.2.jquery.min.js"></script>
<script type="text/javascript" src="path/to/jquery.dataTables.js"></script>
<table id="example" cellpadding="0" cellspacing="0" border="0" >
<thead>
  <tr>
    <th>Name</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Confirmation #</th>
  </tr>
 </thead>
 <tbody>
   <tr></tr>
 </tbody>
</table>

So this is just a simple table, with column names:

  • Name
  • Start Date
  • End Date
  • Confirmation #

Now here’s what you need in your Node Javascript file.  I always name mine node.js.

node.js

function start ()
 {
   var http = require("http");
   var fs = require("fs");
   var express = require('express');
   var app = express();
   var mongo = require("./mongo"); 
   app.configure(function () {
     app.use(express.bodyParser());
     app.use(app.router); app.all('/', function(req, res, next) {
       res.header("Access-Control-Allow-Origin", "*");
       res.header("Access-Control-Allow-Headers", "Content-Type");
       next();
     });
  }); 

  app.get('/mongo/get/datatable', mongo.getDataForDataTable) //Here's the line that we're looking at specifically
  app.use(express.static(__dirname));
  app.listen(process.env.PORT || 8080);
}; 

exports.start = start;

Here’s the line that we really need to look at.

app.get('/mongo/get/datatable', mongo.getDataForDataTable)

So here is our GET request. What this line does, if you’re not really familiar with Node, is it will route a GET request to the method getDataForDataTable in my mongo.js file. Now lets move onto our mongo.js file.

mongo.js

First off, you want these variables to be global.

var mongoose = require('mongoose');
var DataTable = require('mongoose-datatable');
var MyModel;

Configuring is optional, but if you do, make sure you configure the datatable and initialize it on start up.

DataTable.configure({ verbose: false, debug : false });
mongoose.plugin(DataTable.init); var dbURI = 'mongodb://localhost/test';
mongoose.connect(dbURI);

Once again, I set these variables you see below as global, but they don’t have to be, just make sure they can be reached in the db.once method, which we will be writing below.

//Global
var db = mongoose.connection;
var reservations;
var dates; var reservationsSchema;
var dateSchema;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback ()
{
  console.log('Connection has succesfully opened');
  var Schema = mongoose.Schema;
  reservationsSchema = new Schema(
  {
    reservation :
    {
       name : String,
       startDate : String,
       endDate : String,
       confirmationCode : String
    }
 });
 reservations = mongoose.model('reservations', reservationsSchema);
 MyModel = require('mongoose').model('reservations');
});

So here what we’re doing is creating the Schema (reservationsSchema) which is necessary in order to create the model (MyModel) object. The following line retrieves the model.

reservations = mongoose.model('reservations', reservationsSchema);

And in the line below we set the model MyModel, which will be using later.

MyModel = require('mongoose').model('reservations');

So so far we’ve:

  1. Created a small table with the columns specified.
  2. Wrote a router which will route our get request to the proper method in the mongo.js file.
  3. Created our model and schema which both are necessary in order to retrieve the documents from the Mongo database.

Now here is our code to get the data from the Mongo database.

exports.getDataForDataTable = function getData (request, response) {
  //"type.typeName" : "Trolley"
  //console.log("Get Request for Data Table made with data: ", request.query);
  MyModel.dataTable(request.query, function (err, data) {
    response.send(data);
  });
};

So what did I just do? We

  1. Called the dataTable method on the MyModel object, and sent request.query (JSON) as the query for what data we want to receive.  For now, we aren’t going to mess with that query at all, so that will be the default setting.
  2. Returned the data object which contains our documents that we just searched for.

And here’s the final part of our code.

$('#reservationTable').dataTable({
  "bProcessing" : true,
  "bServerSide" : true,
  "sAjaxSource" : '/mongo/get/datatable',
  "aoColumns" : [
    { "mData" : "reservation.name" },
    { "mData" : "reservation.startDate" },
    { "mData" : "reservation.endDate" },
    { "mData" : "reservation.confirmationCode" }
  ]
});

So here’s what we got going on.

  1. bProcessing” : “true” – Enable or disable the display of a ‘processing’ indicator when the table is being processed (e.g. a sort). This is particularly useful for tables with large amounts of data where it can take a noticeable amount of time to sort the entries.
  2. bServerSide” : “true” – Configure DataTables to use server-side processing. Note that the sAjaxSource parameter must also be given in order to give DataTables a source to obtain the required data for each draw.
  3. sAjaxSource” – This is the address of our GET request.
  4. aoColumns” – Used to give DataTables specific instructions for each individual column.
  5. mData” – This property can be used to read data from any JSON data source property, including deeply nested objects / properties.

So basically what I’m doing here is setting the table up to show when it’s processing data, also telling it that we’re going to be doing server side processing, and that the GET request address is /mongo/get/datatable.  Then I’m saying that the column data is going to be taken from the response to the GET request, and we want from the returned JSON, reservation.name, reservation.startDate, reservation.endDate and reservation.confirmationCode. And that’s it, if you run the website now on the server you’ve created, you will see that it’s populated with data.