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!

Upload Folder from Local Machine to EC2 Instance

Uncategorized

This was a problem that I was trying to figure out on my Windows Machine.  Here’s the steps it took for me to be able to do it.

1.  Download an SSH client.  I downloaded OpenSSH.  It’s a free download that you can get from here.

2.  Now check and see if you can SSH into your EC2 instance.   To do this, open up powershell and navigate to the folder where you store your .pem file.  This isn’t necessary, but it’s easier for the tutorial.  Now try to SSH into the instance like so.

ssh -i PemFile.pem ec2-user@xx.xxx.xx.xx

If it was successful you’ll see a screen that looks like this.

The authenticity of host ‘ec2-xxx-xx-xxx-1.compute-1.amazonaws.com (xx.xxx.xxx.xx)’ can’t be established.
RSA key fingerprint is 1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f.
Are you sure you want to continue connecting (yes/no)?

Type in yes.  And you’re logged in.

3.  Now type “exit” so you can leave your remote instance.

4.  Now here’s the command line prompt to actually copy the folder to your remote instance.

scp -i PemFile.pem -r folder/to/copy/ ec2-user@xx.xxx.xx.xx:/destination/folder/to/copy/into/to

You’ll now see the same screen you saw before.

The authenticity of host ‘ec2-xxx-xx-xxx-1.compute-1.amazonaws.com (xx.xxx.xxx.xx)’ can’t be established.
RSA key fingerprint is 1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f.
Are you sure you want to continue connecting (yes/no)?

Type yes, and you’ll see the contents of the folder copying. After it’s done, SSH into your instance and check to see if your folder is where you wanted it to be.

And that’s it!

Amazon EC2 : “Client.AuthFailure: AWS was not able to validate the provided access credentials.”

Uncategorized

Post on Amazon EC2 error I ran into to.

Nasty Code Errors

When trying to get Amazon EC2 CLI working on my Mac I got this error

“Client.AuthFailure: AWS was not able to validate the provided access credentials.”

Here is how I fixed it.

Went to my Amazon Account, and went to Security Credentials.

Added a new Access Keys, x.509 certificate, and a Key Pair and downloaded all the new files.

Then I ran these commands in my mac terminal – continue reading to see actual syntax

sudo chmod 600 [Location of my public key file]/file.pem
sudo chmod 600 [location of my certificate file]/file.pem
sudo chmod 600 [location of my private key file]/file.pem

Here’s what it actually looks like:

The sudo is optional.  You can do a google search and see what the sudo is for.

sudo chmod 600 /Users/user/Documents/Programming/AmazonEC2CLI/pk-XXXXXXXXXXXXXXXXXXXX.pem

sudo chmod 600 /Users/user/Documents/Programming/AmazonEC2CLI/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem

sudo chmod 600 /Users/user/Documents/Programming/AmazonEC2CLI/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem

Now on the Security credentials page, get your Access Key ID and…

View original post 49 more words

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.

How to Deal with Frustrated Employees

Uncategorized

This is a great post for anyone who deals directly, or even indirectly with customers, or your own employees. I think what I appreciate the most is the comments of how really as individuals we must focus on others and not on ourselves.

I’m reblogging this because I know many people who would benefit from this information, especially in my field.

Why I Make Music…

Uncategorized

This is how every craftsman should feel about his craft. If you enjoy what you’re doing, people will know you’re quality before they ever even see what you’ve done. One can learn a lot from those around us!

JamesRadcliffe.com

I make music because of the place that it comes from.  The state I am in when I am making it.  That place where time disappears and everything else dissolves.  The place where fierce joy rises up, unbidden, like golden light filling a well.

That is why I make music.

I believe that this state is communicable thru music.  And that the communication of this state is something as vital and important to us as food, or air, or love.

For me, that is the value of great art.

I am still recording every night; from around midnight till maybe 5 in the morning.

This is my favourite part of the day.  It doesn’t feel like I have a deadline; it feels almost indecently pleasurable, like rolling in ice cream.  The music is a distillation of all the greatest moments from this practice.  It is concentrated joy in a bottle. …

View original post 318 more words

Get More Work Done When Working Remotely

Uncategorized

When beginning to work remotely, your initial reaction is, “THIS IS GREAT!!!”  You get to work when you want, and when you are working, you’re in a comfortable environment, your home.  But there is quickly one problem that you run into.  How do I make sure that I’m productive?  This can be difficult.  For most likely your entire life, you have gone somewhere to work, and people there push you to make sure that you’re doing your job in a timely manner.  Now all of a sudden, that motivation falls upon you.  So how can you make sure that you get more work done when working remotely?  Here are three things that have helped me to be more productive when working remotely.

1) “The plans of the diligent ones surely lead to success.”

Planning ahead is essential to being successful in getting work done.  When you already have in mind a good idea of what you want to accomplish in the day, you will find that you actually do more, because you’re guided by something.  What I do is I use an app called “Simple Daily ToDo List” created by none other than yours truly (sorry for the self-promotion).  But honestly, the reason why I wrote and use this application is so that I can have a very simple way of writing out what I want to do for the day, and then check off what I accomplish as I finish things.  I would actually recommend you getting it, or another app of your choice of course.

It’s a fact that when we write out what we want to accomplish we actually decrease our stress levels because now we don’t have to juggle a lot of things in our minds, and also, once it’s down in writing, you’re more apt to finish it because you have a plan that you can continually refer to which lessens the chances that you deviate from that plan.  Also, it’s very good to be able to see what you accomplished at the end of the day, which is why the app keeps finished tasks there until the next day.  You feel better about yourself when you can look at list of everything that you did.

todolist

Simple Daily ToDo List

Another app that you may want to check out is “Asana”.  Asana is good because you can write out tasks for an entire project.  Asana coupled with Simple Daily ToDo List will make you more productive instantly.  The great thing about this app is it also has a web interface, so you can use web interface on the desktop and it will sync automatically to your phone.

2) “There is benefit in every kind of hard work, But mere talk leads to want”

One of the most difficult things when working remotely is dealing with the fact that your family wants your attention.  The fact of the matter is though, when you’re at home working, it’s the same as working in an office it’s just that you’re at home.  So everyone in the home must understand that when you are working, unless it’s an absolute emergency, you can’t be bothered.  It’s just like when you’re in an office, if someone needs something, or just wants to talk, they have to call you, and they’re probably not going to do that unless it’s absolutely urgent.  So make that known.  Let everyone know that you’re working, and that means you can’t be bothered for trivialities.  This can be tough though (especially if you have kids), so how can you make it a little easier on yourself.

Close the door when you’re working, and tell people that you’re working so please don’t bother you.  You’ll find that this will help you tremendously in getting work done because we all know that when you’re in the groove, the last thing that you want is to be bothered, because once you’re out the groove, it can take a while to get back into it.  Also, one thing that I do is I put on some noise cancellation headphones, and play some music, typically a Pandora station.  This helps you stay relaxed, and it also helps keep out outside noise which will most likely always be there depending on how many people you have in your home.

3) “Better is a handful of rest than a double handful of hard work”

Make sure that you take some time to rest.  I know that sounds obvious, but you have no idea how hard this is.  Generally you want to just beast through a full day, and you realize at the end that you never took a break.  The fact of the matter is you’ll actually accomplish more if you just get up every few hours and do something.  Doesn’t really matter what it is; just give your mind a rest.  This especially is important when you’re stuck at a problem that you can’t solve.  If you walk away for a few minutes and come back, you’ll notice that you feel so much better, and you’ll probably find a solution to that problem you couldn’t solve.  So remember, rest actually helps you to work better.  Don’t overdue this though.  If you rest too much, you’ll get nothing done, but I don’t think I need to tell you that. So there’s a few tips that I hope will help you out.  If you have any other ideas then comment and let the world know what you do.

3 Tips for Simple, Elegant, iOS Design

Uncategorized

As an iOS Developer, you fully one understand one absolutely essential aspect of marketing.

“The apps that are beautiful…sale!!”

The thing is though, not everyone has a limitless supply of funds to pay designers.  And not everyone knows a designer who if you pay him, will actually deliver a quality product.  So the question is, how do you make sure that you’re design is one that will make the end user go…wow!  I’ve put together three simple tips and principles to think about when designing.  In the future, I’ll add more.  Hopefully this will help you in your endeavor of creating beautiful UIs.  At the end of each tip, I’ll show you an example of what I mean for each tip.

Tip 1 – “Imitation is the greatest form of flattery”

It is a fundamental truth that there are many apps out there that are stunningly gorgeous.  When you are going to create an app, why not look at some designs of others.  Here is what I do.

  1. I do a search on the app store for an app similar to the one that I’m about to make.
  2. Then I look at the first say 20 of them, and I narrow it down to about 3 that I find most beautiful.
  3. Then I just analyze the three apps and try to determine what makes them so elegant.
  4. Then I read the reviews and see what others are saying about them.   (Customers know what they want and they’ll be honest if they want to see something different.)
  5. After all this, I generally have an idea of what I want, and I begin my mock up.

The great thing about this system is that you have an idea of what others have already done that worked and that did not work.  Also, I find it easier to build something after I’ve seen something that I can kind of build off of.  Now this isn’t copying, it’s simply imitating.  Eventually you’ll be happy that others are imitating your app designs.

So let’s see an example.  Look at the Yahoo Weather.  What makes this so nice?

yahooweatherapp

1)      Beautiful background pictures?

2)      Fonts are easy to see on the pictures because they make sure the images they use have dark areas where there is text?

3)      The icons are very consistent, and very simple?

4)      They utilize Flat UI?

5)      The font selections are perfect?

So then you would strive to imitate this screen, of course not an exact replica, but strive to use beautiful background pictures with well-placed fonts with good font selections, and Flat UI.

Tip 2 – “Keep it simple Socrates”

“Wisdom is with the modest one.”  What does that mean?  It means you have to understand your limitations.  If you’re not a graphics designer, don’t try to make some epic design with all kinds of rotating images and parallax scrolling, with particle affects.  All that’s going to happen is you’re going to have a jumbled up bunch of nonsense.  Instead, focus on simplicity.  Instead of having 20 things on the screen that are all kind of just thrown together, have 3 things done beautifully.   A good example of this is an app made by Fueled called Elevatr.  Look at the app screenshots below.

elevatr

Do you notice how simple the first screen shot is?  There’s nothing fancy at all about this.  It’s simply a static picture with some text, but it does its job.  The second screenshot, is extremely elegant but simple, it’s simply just a table with some text.

Look at the 3rd screenshot.  What makes this screen so elegant?  Notice the user of colors makes the app very pleasing to the eye.  The way they have the information compressed in the boxes, and when you want to view all the information you SIMPLY click on the plus sign.

Now what makes all this so nice though, it’s well thought out colors, a well thought out image, and well thought out font selections and locations.  That’s it!!  Nothing fancy!!

Now ask yourself.  Could I do this?

The answer is an emphatic YES!!!

That’s what makes this so awesome.  It’s not overly complicated.   Simplicity is often better then complexity.

Tip 3 – “What we see depends mainly on what we look for.”

If you want to have good designs, it is very important that you keep your eyes open for good designs in everything that you see.   What do I mean by that?  Well, it’s interesting how everything that we see, really has some sort of design, you just need to keep your eye open for it.  For example, when I went to CES in 2014, I was taking pictures of everything from cars to the carpet selection.  Why?  Because hidden there was design.

mustang

Look at this picture of a mustang interior.  Now, this looks like a beautiful interior, right?  Why not use these colors for a color scheme of an app?  Metallic silver, yellow, black, with maybe some ribbons of red and white.  You’d be amazed at how something like that could look!

Do you get the idea?  Use designs that you may have not thought of to give you ideas for an app.

So these are just a few tips that I wanted to share to help people to get some killer designs for their apps.  If anyone has any good links to their own designs, or to some cool tips, just leave a link or something in the comment.  Hope that this helps everyone who reads it.