Getting Started With Node.js

By: Matthew Poulson

Node Expert

For about a month

What is it?

"It is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices."

https://nodejs.org/

Node Approved for Web Application Service Layer

Why Use Node

  1. Front end Developers know JavaScript
  2. Node apps can be Isomorphic

Who uses Node

https://www.talentbuddy.co/blog/building-with-node-js/

https://www.talentbuddy.co/blog/building-with-node-js-at-linkedin/

https://www.talentbuddy.co/blog/building-with-node-js-at-netflix/

https://www.talentbuddy.co/blog/building-with-node-js-at-the-new-york-times/

https://www.talentbuddy.co/blog/on-building-with-node-js-at-medium-and-growing-as-a-developer/

Node Stack Starter

Prep your box

  1. Get git
  2. Tell git to use https instead of the git protocol git config --global url."https://".insteadOf git://
  3. Install nodejs
  4. Install gulp, yeoman, bower npm install -g yo gulp bower
  5. Point NPM at our internal registry
  6. npm set registry http://icsnpm.ldschurch.org/
  7. Install the mwstack generator
  8. $ npm install -g generator-lds-node

Run: 'yo lds-node'

That's it

You're done

Seriously

Node Basics

  • Request & Response
  • Modules

Express

  • Middleware
  • Routes
  • Templates

Request & Response

  1. Created when node receives a request
  2. Objects
  3. Hold data about the Request & the Response

Request & Response

Middleware

Modules

Templates

Routes

Routes

  1. Compare the Path of the Request object 
  2. Run a function

Request & Response

Middleware

Modules

Templates

Routes

router.get('/', function(request, response) {
 response.render('index', { 
    title: 'test' 
  });
});

Example Route

Middleware

Modules

Templates

Routes

Request & Response

router.get('/:name', function(request, response) {
    var name = request.params.name;
        response.render('index', {
            username: name
        });
});



// path: '/tom',

// name = tom

Route with Parameter

Middleware

Modules

Templates

Routes

Request & Response

Templates

response.render('index', { 
    title: 'test' 
  });
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel="stylesheet" href="styles/style.css" />
  </head>
  <body>
    <div>
        Hello World, this is the 
        <%= title %> project
    </div>

    <script src="scripts/test.js"></script>
  </body>
</html>

Middleware

Modules

Templates

Routes

Request & Response

Middleware

Functions that receive and modify the Request & Response objects

Middleware

Modules

Templates

Routes

Request & Response

next() or response.end()

Middleware

Modules

Templates

Routes

Request & Response

app.use(json());
app.use(urlencoded({
  extended: true
}));

app.use(helmet.xframe('deny'));
app.use(helmet.csp({
  'default-src': ["'self'", '*.ldscdn.org']
  , styleSrc: ["'self'", "'unsafe-inline'", '*.ldscdn.org']
}));
app.use(helmet.xssFilter());
app.disable('x-powered-by');

app.use(wam());
app.use(express.static(path.join(__dirname, 'public')));

Registering Middleware

Middleware

Modules

Templates

Routes

Request & Response

// /routes/index.js

//Stuff it Requires
var router = require('express').Router();

//Stuff it Does
router.get('/', function(request, response) {
console.log('The req object:', request);
  response.render('index', { 
    title: 'test' 
  });
});

//Stuff it Exports
module.exports = router;

Modules

Middleware

Modules

Templates

Routes

Request & Response

function sluggify (title) {
    var slug = encodeURIComponent(title.replace(/\s/g, '-')
                                   .replace(/&/, 'and')
                                   .replace(/\W+/, '')
                                   .toLowerCase());
    return slug;
}
module.exports = sluggify;

Module Example

Middleware

Modules

Templates

Routes

Request & Response

Installing Modules

Middleware

Modules

Templates

Routes

Request & Response

npm install <package name>

 

Installs the module in your node_modules directory

Middleware

Modules

Templates

Routes

Request & Response

npm install <package name> -g

Installs the module globally

Middleware

Modules

Templates

Routes

Request & Response

npm install <package name> --save

Installs the module in your node_modules directory and adds it to your package.json under dependencies

Middleware

Modules

Templates

Routes

Request & Response

npm install <package name> --save-dev

Installs the module in your node_modules directory and adds it to your package.json under devDependencies

Middleware

Modules

Templates

Routes

Request & Response

npm install

Installs all of the modules listed as a dependency or dev dependency in your package.json

Middleware

Modules

Templates

Routes

Request & Response

Including modules

require('<package name>')

Loads the named module

// npm module
express = require('express'),

// local module
routes = require('./routes'),

Middleware

Modules

Templates

Routes

Request & Response

Conclusion

Is a real boy

Node

By Matthew Poulson

Node

  • 1,140