Skip to content

Commit 041f3a8

Browse files
committed
working build
1 parent 474f02e commit 041f3a8

File tree

5 files changed

+130
-62
lines changed

5 files changed

+130
-62
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gem 'github-pages'

Gemfile.lock

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
RedCloth (4.2.9)
5+
blankslate (2.1.2.4)
6+
classifier (1.3.4)
7+
fast-stemmer (>= 1.0.0)
8+
colorator (0.1)
9+
commander (4.1.6)
10+
highline (~> 1.6.11)
11+
fast-stemmer (1.0.2)
12+
ffi (1.9.3)
13+
github-pages (15)
14+
RedCloth (= 4.2.9)
15+
jekyll (= 1.4.3)
16+
kramdown (= 1.3.1)
17+
liquid (= 2.5.5)
18+
maruku (= 0.7.0)
19+
rdiscount (= 2.1.7)
20+
redcarpet (= 2.3.0)
21+
highline (1.6.21)
22+
jekyll (1.4.3)
23+
classifier (~> 1.3)
24+
colorator (~> 0.1)
25+
commander (~> 4.1.3)
26+
liquid (~> 2.5.5)
27+
listen (~> 1.3)
28+
maruku (~> 0.7.0)
29+
pygments.rb (~> 0.5.0)
30+
redcarpet (~> 2.3.0)
31+
safe_yaml (~> 0.9.7)
32+
toml (~> 0.1.0)
33+
kramdown (1.3.1)
34+
liquid (2.5.5)
35+
listen (1.3.1)
36+
rb-fsevent (>= 0.9.3)
37+
rb-inotify (>= 0.9)
38+
rb-kqueue (>= 0.2)
39+
maruku (0.7.0)
40+
parslet (1.5.0)
41+
blankslate (~> 2.0)
42+
posix-spawn (0.3.8)
43+
pygments.rb (0.5.4)
44+
posix-spawn (~> 0.3.6)
45+
yajl-ruby (~> 1.1.0)
46+
rb-fsevent (0.9.4)
47+
rb-inotify (0.9.3)
48+
ffi (>= 0.5.0)
49+
rb-kqueue (0.2.2)
50+
ffi (>= 0.5.0)
51+
rdiscount (2.1.7)
52+
redcarpet (2.3.0)
53+
safe_yaml (0.9.7)
54+
toml (0.1.1)
55+
parslet (~> 1.5.0)
56+
yajl-ruby (1.1.0)
57+
58+
PLATFORMS
59+
ruby
60+
61+
DEPENDENCIES
62+
github-pages

_posts/2014-2-17-javascript-gotchas.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ tags:
77

88
# JavaScript gotchas
99

10-
JavaScript is a language that many developers are familiar with but lack a deep understanding. The language lends itself to new programmers because its simple syntax. Many of the core fundamentals can be avoided provided that the application being built is sufficiently simple. The goal of this lab is to give you a better understanding of some core concepts and things to look for with JavaScript.
10+
JavaScript is a language that many developers are familiar with but lack a deep understanding. The language lends itself to new programmers, in part, because of its simple syntax. Many fundamental concepts and features of the language can be overlooked with a simple procedural approach. The goal of this lab is to give you a better understanding of some core concepts and important language features to understand about JavaScript.
1111

12-
## What's ```this```?
12+
## What is ```this```?
1313

1414
Have a look at this snippet and pay special attention to the use of the keyword ```this```.
1515

@@ -31,43 +31,46 @@ var obj = {
3131
obj.doSomething();
3232
{% endhighlight %}
3333

34-
Those unfamiliar with how ```this``` is handled may think the call to ```obj.doSomething``` might think the output of running the above snippet is this:
34+
Those unfamiliar with how ```this``` is handled may think the call to ```obj.doSomething``` would output the following (assuming the snippet above is saved in a file named `this.js`:
3535

36-
```
36+
{% highlight bash %}
37+
> node this.js
3738
Name: bob
3839
Name: bob
39-
```
40+
{% endhighlight %}
41+
42+
This is a very common mistake. What is actually output is this:
4043

41-
What's actually output is this:
4244

43-
```
45+
{% highlight bash %}
46+
> node this.js
4447
Name: bob
4548
Name: undefined
46-
```
49+
{% endhighlight %}
4750

48-
Here's another example and then we'll explain what's going on.
51+
Here is another example and then we will explain what is going on.
4952

5053
{% highlight javascript %}
5154
function Adder (a) {
5255
this.a = a;
5356
};
54-
57+
5558
Adder.prototype.addAsync = function (b) {
5659
setTimeout(function () {
5760
console.log(this.a + b);
5861
}, 10);
5962
};
60-
63+
6164
var r = new Adder(5);
6265
r.addAsync(10);
6366

6467
{% endhighlight %}
6568

66-
This example is supposed to add two numbers together after 10 milliseconds. What's the output of this example? Some may think it would print 15, the result of 5 + 10. The correct answer is ```NaN```. Why? The answer lies in the value of ```this```.
69+
This example is supposed to add two numbers together after 10 milliseconds. What is the output of this example? Some may think it would print 15, the result of 5 + 10. The correct answer is ```NaN```. Why? The answer lies in the value of ```this```.
6770

68-
If there's anything you'll walk away from after this lab I hope you'll no longer write bugs that involve ```this```.
71+
If there is anything you will walk away from after this lab I hope you no longer write bugs that involve ```this```.
6972

70-
The value of ```this``` is the object that a function is defined on. Inner functions or function calls that aren't a part of the object will have the default object set to ```this```. The default object in browsers is ```window``` and in Node.js is ```global```.
73+
The value of ```this``` is the object that a function is defined on. Inner functions or function calls that are not a part of the object will have the default object set to ```this```. The default object in browsers is ```window``` and in Node.js is ```global```.
7174

7275
{% highlight javascript %}
7376
var obj = { a: "Example" };
@@ -84,15 +87,15 @@ printer() // => undefined
8487
obj.p() // => Example
8588
{% endhighlight %}
8689

87-
Notice how the value of the call to ```printer``` that wasn't attached to an object was ```undefined```. Once we set the property p to the printer function and invoke it you'll see that this now refers to the object defined. When an object is created from a constructor function using the new keyword, a brand new object is set to ```this```. For example:
90+
Notice how the value of the call to ```printer``` that was not attached to an object was ```undefined```. Once we set the property p to the printer function and invoke it you will see that this now refers to the object defined. When an object is created from a constructor function using the new keyword, a brand new object is set to ```this```. For example:
8891

8992
{% highlight javascript %}
9093
var ctor = function (a) {
9194
this.a = a;
9295
};
9396

9497
ctor.prototype.print = function () {
95-
console.log(this.a);
98+
console.log(this.a);
9699
};
97100

98101
var o1 = new ctor('test1');
@@ -102,7 +105,7 @@ var o2 = new ctor('test2');
102105
o2.print(); // => test2
103106
{% endhighlight %}
104107

105-
So, you've seen the default behavior of how JavaScript handles the ```this``` keyword. The value of ```this``` can be controlled in a function call via a few methods available on Function.prototype ```apply```, ```call```, and ```bind```. Each of these methods allow you to modify the value of this when the function is called. The first two options (apply and call) invoke the function immediately, whereas the third (bind) provides a new function with ```this``` bound. The value of ```this``` is the first argument to each of these methods. Here's an example:
108+
So, you have seen the default behavior of how JavaScript handles the ```this``` keyword. The value of ```this``` can be controlled in a function call via a few methods available on Function.prototype ```apply```, ```call```, and ```bind```. Each of these methods allow you to modify the value of this when the function is called. The first two options (apply and call) invoke the function immediately, whereas the third (bind) provides a new function with ```this``` bound. The value of ```this``` is the first argument to each of these methods. Here is an example:
106109

107110
{% highlight javascript %}
108111
var obj = { a: "Example" };
@@ -119,30 +122,30 @@ printer.apply(obj); // => "Example"
119122
printer.call(obj); // => "Example"
120123
{% endhighlight %}
121124

122-
The difference between ```apply``` and ```call``` is that apply allows you to invoke the function with the arguments as an array; ```call`` requires the parameters to be listed explicitly.
125+
The difference between ```apply``` and ```call``` is that `apply` allows you to invoke the function with the arguments as an array; ```call``` requires the parameters to be listed explicitly.
123126

124127

125128
# Var
126129

127-
The keyword ```var``` is used to define variables. Unfortunately, JavaScript does not require the use of this keyword when defining variables. Forgetting to leave off the var keyword can pollute the global object with unnecessary properties. It can also create innocent looking bugs. Have a look at the following example, What's the output?
130+
The keyword ```var``` is used to define variables. Unfortunately, JavaScript does not require the use of this keyword when defining variables. Forgetting to leave off the var keyword can pollute the global object with unnecessary properties. It can also create innocent looking bugs. Have a look at the following example, What is the output?
128131

129132
{% highlight javascript %}
130133
function doStuff() {
131134
for (i = 0; i < 5; i++) {
132135
console.log(i);
133136
}
134137
}
135-
138+
136139
function example() {
137140
for (i = 0; i < 5; i++) {
138141
doStuff();
139142
}
140143
}
141-
144+
142145
example();
143146
{% endhighlight %}
144147

145-
At first glance it looks like it would output the numbers 0 to 5 - 5 times. Sadly, it doesn't; instead it outputs the number 0 - 5 just once! What's the problem? It's the fact that this example omits the use of the var keyword. To fix this we must declare the loop control variables within the function. Fixing the above example looks like this (note the use of var):
148+
At first glance it looks like it would output the numbers 0 to 5 - 5 times. Sadly, it does not; instead it outputs the number 0 - 5 just once! What is the problem? It is the fact that this example omits the use of the var keyword. To fix this we must declare the loop control variables within the function. Fixing the above example looks like this (note the use of var):
146149

147150
{% highlight javascript %}
148151
function doStuff() {
@@ -151,22 +154,22 @@ function doStuff() {
151154
console.log(i);
152155
}
153156
}
154-
157+
155158
function example() {
156159
for (var i = 0; i < 5; i++) {
157160
doStuff();
158161
}
159162
}
160-
163+
161164
example();
162165
{% endhighlight %}
163166

164-
Using JavaScripts strict mode will prevent you from defining properties on the global object. There are other advantages to using strict mode, but this one is my favorite. Strict mode is applied within an execution context instead of over the entire JavaScript VM. To enable strict mode simple include the string ```"use strict";``` at the top of an execution context. Here's an example of using strict mode just for a single function.
167+
Using JavaScripts strict mode will prevent you from defining properties on the global object. There are other advantages to using strict mode, but this one is my favorite. Strict mode is applied within an execution context instead of over the entire JavaScript VM. To enable strict mode simple include the string ```"use strict";``` at the top of an execution context. Here is an example of using strict mode just for a single function.
165168

166169
{% highlight javascript %}
167170
var strictFunction = function () {
168171
"use strict";
169-
x = 1; // => Throws error because of strict mode!
172+
x = 1; // => Throws error because of strict mode!
170173
};
171174

172175
var notSoStrictFunction = function () {
@@ -179,9 +182,9 @@ var notSoStrictFunction = function () {
179182

180183
{% highlight javascript %}
181184
var input = "10";
182-
183-
if (input == 10) {
185+
186+
if (input == 10) {
184187
console.log(input * 5);
185-
console.log(input + 5);
188+
console.log(input + 5);
186189
}
187190
{% endhighlight %}

_posts/2014-2-19-deploying.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,47 @@ EC2 -> Launch Instance -> Free tier -> Ubuntu Server 13.10 - ami-ace67f9c (64-bi
1414

1515
Locate the public ip address for the virtual machine you just created. You can ssh into it with the following command. Assuming your .pem file is in the same directory as you're running the command.
1616

17-
```
17+
{% highlight bash %}
1818
ssh -i node.pem ubuntu@54.213.246.161
19-
```
19+
{% endhighlight %}
2020

2121
## Installing Node
2222

2323
Node has great instructions on how to install from apt-get [here](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager). The important part is here:
2424

25-
```
25+
{% highlight bash %}
2626
sudo apt-get update
2727
sudo apt-get install -y python-software-properties python g++ make
2828
sudo add-apt-repository ppa:chris-lea/node.js
2929
sudo apt-get update
3030
sudo apt-get install nodejs
3131

3232
sudo ln -s /usr/bin/node /usr/bin/nodejs
33-
```
33+
{% endhighlight %}
3434

3535
This sets up a new aptitude repository where the most recent stable version of node should be available. At the time of this writing the official ubuntu repositories had an out-dated version of node.
3636

3737
Verify your installation
3838

39-
```
39+
{% highlight bash %}
4040
node -v
41-
```
41+
{% endhighlight %}
4242

4343
# Setting up a node process
4444

4545
Let's start by creating a simple node web application using express. From your home directory:
4646

47-
```
47+
{% highlight bash %}
4848
> mkdir node_app
4949
> cd node_app
5050
> npm init
51-
```
51+
{% endhighlight%}
5252

5353
Run through the prompts to initialize your package.json file.
5454

55-
```
55+
{% highlight bash %}
5656
npm install express --save
57-
```
57+
{% endhighlight %}
5858

5959
This will install express and update your package.json file.
6060

@@ -81,20 +81,21 @@ console.log('Server listening on port ' + app.get('port'));
8181

8282
Let's start the process and run a quick curl request. Notice that the port specified is 3000.
8383

84-
```
84+
{% highlight bash %}
8585
node server.js&
8686
curl -I localhost:3000
87-
```
87+
{% endhighlight %}
8888

8989
Now kill the process and let's continue.
9090

91-
```
91+
{% highlight bash %}
9292
killall node
93-
```
93+
{% endhighlight %}
9494

9595
## Cluster
9696

97-
We'll start scaling Node using a built in technique. The cluster module that's part of the core node library is an option. In the current version of node (v0.10.*) the cluster module doesn't appropriately route traffic to child processes. Node leaves the routing of incoming connections to the operating system. Unix based operating systems do a poor job of distributing new connections amongst child processes. Windows is not affected by this issue. In the next stable version of Node the distribution of incoming connections will be handled by node and by default use a round-robin approach. Despite the fact that it's not a great choice to use at this time we're going to cover how it works.
97+
We'll start scaling Node using a built in technique. The cluster module that's part of the core node library is an option. In the current version of node (v0.10.\*) the cluster module doesn't appropriately route traffic to child processes. Node leaves the routing of incoming connections to the operating system. Unix based operating systems do a poor job of distributing new connections amongst child processes. Windows is not affected by this issue. In the next stable version of Node the distribution of incoming connections will be handled by node and by default use a round-robin approach. Despite the fact that it's not a great choice to use at this time we're going to cover how it works.
98+
9899

99100
### Master and child
100101

@@ -144,57 +145,57 @@ app.set('port', process.env.PORT || 3000);
144145

145146
Now when we launch our process we can set the PORT environment variable. Let's try it by running passing different ports to a few node processes.
146147

147-
```
148+
{% highlight bash %}
148149
PORT=4000 node server.js&
149150
PORT=4001 node server.js&
150-
```
151+
{% endhighlight %}
151152

152153
and test them:
153154

154-
```
155+
{% highlight bash %}
155156
curl -I localhost:4000
156157
curl -I localhost:4001
157-
```
158+
{% endhighlight %}
158159

159160
and kill them
160161

161-
```
162+
{% highlight bash %}
162163
killall node
163-
```
164+
{% endhighlight %}
164165

165166
Alright, now we see how we can configure node process to listen on different ports via an environment variable. Let's see how to manage the lifetime several processes.
166167

167168
## Forever
168169

169170
Forever is an executable that's responsible for keeping a given process alive. If the process crashes, it's the responsibility of forever to start it again. Processes will die. This is a fundamental fact. It's worth mentioning that if a process encounters an unexpected exception it should terminate completely. This is recommended because the state of the process may be unknown. It's usually just as easy to start a brand new process than to deal with one in possibly an invalid state.
170171

171-
```
172+
{% highlight bash %}
172173
sudo npm install forever -g
173-
```
174+
{% endhighlight %}
174175

175176
Now we can use forever to ensure our node server continues running.
176177

177-
```
178+
{% highlight bash %}
178179
PORT=4000 forever start server.js
179-
```
180+
{% endhighlight %}
180181

181182
Now, let's find the process id of the newly started process and kill it.
182183

183-
```
184+
{% highlight bash %}
184185
ps -ax | grep node
185-
```
186+
{% endhighlight %}
186187

187188
You'll see a line like this:
188189

189-
```
190+
{% highlight bash %}
190191
7076 ? Sl 0:00 /usr/bin/nodejs /home/ubuntu/node_app/server.js
191-
```
192+
{% endhighlight %}
192193

193194
Let's kill that process.
194195

195-
```
196+
{% highlight bash %}
196197
kill -9 7076
197-
```
198+
{% endhighlight %}
198199

199200
Re-run the process grep and you should see a new process id assigned to the node process! Great! Now we have a way to keep the process running. The next step handle keeping processes running after a system restart.
200201

0 commit comments

Comments
 (0)