/Dev-NodeJs

NodeJs Tutorial

Primary LanguageJavaScript

node.js for devs


Get dangerous w/ Node in a day

Today we will:

  • Get up and running with nvm/n, npm, Node.js and Express.
  • Learn fundamental Node and JavaScript concepts like callbacks, events, and promises.
  • Become familiar with some of Node's most popular modules and learn their benefits.
  • Bootstrap a site and API with the latest tools like yeoman, bower, and others.
  • Deploy your site to heroku or a cloud of your choice.

And more importantly:

  • Teach you where to look in order to more easily understand and learn Node on your own.

9am-9:30am: Getting Setup

(For anyone who isn't yet)


9:30am-10:15am: JavaScript Refresher

brief language overiew, callbacks, events, and promises


9:30am-10:15am: JavaScript Refresher


15 min break


10:30am - 11:15am: Groking Node


11:15am - 12:00pm: NPM and Modules


12:00pm - 1:00pm: Lunch


1:00pm - 1:45pm - Common Core Modules


1:00pm - 1:45pm - Common Core Modules


1:45pm - 3:00pm - Common Userland Modules

  • 'npm home express', connect, jade
  • 'npm home underscore' (or lodash), async, bluebird
  • 'npm home mocha', request, mongoose, passport.js
  • Pro Tip: Favor modules that do one thing, but do it well. Compose these modules into larger modules and solutions.
  • Pro Tip: Navigating and learning to use modules - View the tests and examples!
    • learn connect and its usage inside express, by navigating express' node_modules
    • Note: Express 4.0 will not include middleware anymore. You'll need to include each in your package.json. This makes express, itself, and your apps more maintainable.

15 min break


1:55pm - 3:00pm - Common Userland Modules

Problem:

    1. Create a simple express app that lets you upload an image, which is then shown to the user. (To complete this you'll create a simple express site, add routes and views which present a form to a user and accept a multipart POST from the form.)
  • Bonus 1) use the 'gm' module to resize your images upon upload to save a thumbnail as well as original message.
  • Bonus 2) use the 'async' module to create three different sizes of your uploaded image.

More Learning:


3:00 - 3:45pm - Start Hacking


3:00 - 3:45pm - Start Hacking

Problems:

    1. Install yeoman, install the 'browserify' generator and generate a new site. Choose gulp when asked to do so. (if you have issues with SASS, redo and choose not to install when generating).
    1. Update the site you just created to make an ajax call back to the server. Do a GET request to a new route in your site and return a random word to the browser. When received in the browser, update the page to say Hello+Your_Word instead of 'Hello World'. Make the random-word-swap happen every time someone clicks the 'Learn more' button. User browserify to keep your code well organized.
    1. Separate your API from your site by creating a new express site and a proxy using http-proxy. Keep each in a separate folder and start each individually, on different ports. Use a regular expression to have your proxy direct all requests with a url including '/api' to the api, and all other requests to your site.

3:45pm - 4:00pm - Deploying to heroku

  • adding a Procfile
  • starting multiple executables

Problem:

  • create a Procfile, heroku create, git push heroku master

More Learning:


async: waterfall

async.waterfall([
  function(callback) {
    callback(null, 'one', 'two');
  },
  function(arg1, arg2, callback) {
    // Here arg1 is 'one' and arg2 is 'two'
    callback(null);
  },
], function(err, results) {
  // Optional callback when everything is finished
});

node.js: cluster

var os = require('os');
var cluster = require('cluster');

cluster.setupMaster({
  exec: 'app.js'
});

cluster.on('exit', function(worker) {
  console.log('Worker ' + worker.id + ' died');
  cluster.fork();
});

for (var i = 0; i < os.cpus().length; i++) {
  cluster.fork();
}

Express REST API

File Upload

CSRF Protection