Learning Node.js Lab

 

x.co/nodejs

 

Guy Ellis / @wildfiction

Slides: bit.ly/node-lab

Who is Guy Ellis?

  • Writing code for decades
  • Currently focusing on Node.js/JavaScript development
  • Gun for hire
  • Twitter @wildfiction

Agenda

  • Long intros bore me so...
  • ...dive in and write some code
  • How to learn Node.js
  • Compare written code to HTTP Status Check
  • If time permits - testing/questions

Tools Installed?

 

          Open a console/command/terminal window.

          Type: node -v

          Expect: v0.10.31 or similar

          Type: npm -v

          Expect: 2.1.3 or similar

          
  • Create an empty directory

    • e.g. "nodelab"

  • Create a file called index.js

  • Open it in an editor.

    • e.g. subl index.js

 

var request = require('request');
request('http://linksilk.com', function(error,response,body){
  console.log('linksilk.com: ', response.statusCode);
});
  

Replace http://linksilk.com with one of your websites/URLs.


Save file.

Run file:

 

node index.js

Expect error:

 

          module.js:340

          throw err;

          ^

          Error: Cannot find module 'request'
          

npm install request

Expect:

 

200 (or other HTTP status)

 

What just happened?

Why is this of value to you?

Duplicate 3 request lines and change second set of lines to a different URL.

 

          Use www.linksilk.com

  • Expecting 301 for www result and not 200.

  • What is the result in a browser?

  • Why did this happen?

Change second request to use an object:

 

var options = {
   followRedirect: false,
   url: 'http://www.linksilk.com'
};
          

Back to example

 

Append: console.log('done');

Run: node index.js

          

Why does "done" before other results?

How to Learn Node.js

  • Tinker with this file.
  • Add more sites you are interested in.
  • Think of more tests you can do
  • Should we test for 404s?

Open http-status-check project

 

Review samplesites.js

 

compare node_modules folders

     

Open /lib/uriCheck.js

 

Find and compare common sections

GitHub

 

How do forks, clones and pull requests work?

 

Why get involved?

Testing

Compiling?

Testable index.js

var request = require('request');
module.exports.run = function(callback) {
    request('http://linksilk.com', function(error,response,body){
        if(response.statusCode === 200) {
            return callback('_ linksilk.com successful');
        } else {
            return callback('X linksilk.com expected 200 and got ' + response.statusCode);
        }
    });
};

module.exports.run(function(result){
    console.log(result);    
});

 

var assert = require('assert');

var index = require('./index');
describe('test suite', function(){
    it('should run all the tests', function(done){
        index.run(function(result){
            assert.equal(result, '_ linksilk.com successful');
            done();
        });
    });
});

 

test.js

rewire

var rewire = require('rewire');
var index = rewire('./index');

        index.__set__('request', function(url, callback) {
            callback(null, { statusCode: 200 });
        });

npm install rewire

Questions?

 

Guy: @wildfiction

 

 

Made with Slides.com