Node.js Documentation
Node.js Documentation
Introduction

Node.js

– Which is what's everyone is building these days (-:

Philosophy

– Another characteristic of many Node.js modules is the fact that they are created to be used rather than extended

– Uncomplicated, practical approach is preferred here upon a pure, flawless design.

NPM

Node Packaged Modules

npm express-packedges

Starting an NPM project

  • npm init

    – npm init –yes

    ask me no question I tell you no lies mode.

  • npm install

    – Shortcut: npm i

    – Example: npm i lodash

  • npm ls

    – Lists the dependencies

    – To show only top level:

    npm ls --depth=0

Note: npm installs from local cache (instead of network) if exists

Using Lodash

Lodash is a popular utility library, it has some useful functions, lest see few of them.

e.g. here is the times() function:

function getRandomScore() {

   return Math.round(Math.random() * 100);

}

var result = _.times(5, getRandomScore);

// result => [64, 70, 29, 10, 23]

Using Lodash - keyBy

Making a map from an array of objects:

var posts = [

   { id: '1abc', title: 'First blog post', content: '...' },

   { id: '2abc', title: 'Second blog post', content: '...' },

];

var postsMap = _.keyBy(posts, 'id');

var post = postsMap['2abc']

// post -> { id: '2abc', title: 'Second blog post', content: '...' }

Using Lodash - debounce

Debouncing events:

var elEmail = document.getElementById('email-input');

elEmail.addEventListener('keyup', _.debounce(validateEmail, 500));

Node.js Module system

To include a module, use the require() function:

var colors = require('colors');

console.log('Hello'.green);

console.log('I like it when in rains'.underline.red)

console.log('inverse the color'.inverse);

console.log('OMG Rainbows!'.rainbow);

Lets try some more:

Creating your own module:

function sayHello() {

   console.log('Hello')

}

function getAns() {

   return axios.get('https://yesno.wtf/api')

     .then(res => res.data)

}

module.exports = {

         sayHello,

         getAns

}

Globals

Remember that:

global state should be avoided whenever possible

// Node.js global vars are put on the 'global' object

global.userName = 'Puki';

__dirname – global that contains the curr folder path, full path

Nodemon

Used in development to re-run the app on every file change. (Its like live-server for your node.js)

Install using:

npm install –g nodemon

nodemon --ignore "./data" server.js

Async IO is everywhere

There are different ways to do I/O in Node.js, asynchronous IO is very common in node:

fs.stat(fileName, cb)

fs.readFile(fileName,[options], cb)

fs.watch(filename[, options][, cb])

// reading a big file with stream

stream = fs.createReadStream('bigfile.txt');

stream.on('data', function(data){});

The resolving algorithm
Debug

Easy, with VSCode!

Debugging Screenshot
Node techniques

continuation-passing style (CPS)

function sum(a, b) {

   return a + b;

}

function sum(a, b, cb) {

   cb(a + b);

}

Conventions - Callbacks come last

For the sake of readability, a callback param to a function will always come last, example:

fs.readFile(filename, [options], callback)

Conventions - Errors

Error comes first:

– The error produced by a CPS function is passed as the first argument of the callback (null or undefined when no errors)

– Any actual result is passed starting from the second argument.

– Example:

fs.readFile('puki.txt', function(err, data) {

   if ( err ) handleError(err);

   else processData(data);

});

Propagating Errors

function getJSONFromFile(filename, cb) {

   fs.readFile(filename, function(err, data) {

     if ( err ) return cb(err);

     var json;

     try {

         json = JSON.parse(data);

     } catch (e) {

         return cb(e);

     }

     cb(null, json); // everything went fine

   });

};

Require a JSON file

Actually, reading a JSON file is easy with:

var todos = require('../data/todo.json')

A Simple Node Server

A Simple Node Server

var http = require('http');

http.createServer(function (req, res) {

   res.writeHead(200, {'Content-Type': 'text/plain'});

   res.end('Hello misterBIT\n');

}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

Introducing Express.js

Express is a fast, unopinionated, minimalist web framework for Node.js

const express = require('express')

const app = express()

app.get('/', (req, res) => res.send('Hello!􀅨))

app.listen(3000, () => console.log('Server listening on port 3000!'))

Server side rendering

In Node, server side rendering can be done with the module ejs

app.get('/admin', (req, res) => {

   res.render(VIEW_DIR +'admin.ejs',

     { greet: 'Yes Master', users : theUsers });

})

<%= greet %>

<ul>

   <% users.forEach(function(user){ %>

   <li>

     <%=user.fullName %>

   </li>

   <% }); %>

</ul>

Submitting Forms

Using HTML Forms:

<form action="/todo/save" method="post">

   <input name="id" type="hidden" value="<%=todo.id %>" />

   <input name="txt" type="text" value="<%=todo.txt %>" />

     <label>

       <input name="completed" value="true" type="checkbox"

         <%= (todo.completed)? 'checked' : '' %> />

     Done

  &</label>

  &<button type="submit">Save

</form>

Express bodyParser

Express bodyParser is the module responsible for handling requests body:

// Support forms submit

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

// Than in an app.post():

const formData = req.body

Cookies

Cookies Usages

Cookies are mainly used for three purposes:

Using Cookies

Memory / Permanent Cookies

  • Cookie are deleted when browser closes, or they can have an expire time
  • res.cookie(lastVisitedTodoId, todo.id, {maxAge: 60*60*1000});

  • You can view the cookies in the developer tools
  • Cookies in Dev Toolse
    Session

    app.use(cookieParser());

    var session = require('express-session')

    app.use(session({

       secret: 'puki muki',

       resave: false,

       saveUninitialized: true,

       cookie: { secure: false }

    }))

    app.post('/setUser', (req, res) => {

       req.session.nickname = req.body.nickname

       res.redirect('/todo');

    })

    The Cookie and the Session

    The Cookie and the Session
    NodeJS REST API
    REST API

    JSON – JS Object Notation:

    • JSON is a self-describing, human readable data format.
    • Originally designed for lightweight exchanges between browser and server, it has become widely accepted for many types of applications.
    JS Object Notation

    REST APIs:

    Postman

    Postman

    Login / Signup