Thursday, March 26, 2020

Visit Count Session in Node and Express

        if(!request.session.visitcount){
            request.session.visitcount = 0;
        }
        request.session.visitcount += 1
        console.log(`Number of visits: ${request.session.visitcount}`)

Sunday, March 22, 2020

Node JS Tricks


  • If Cannot install thirdparty application via terminal, you will need package.json file for that.

To install package.json file, write this in your terminal

npm init

  • For automated loading of console in visual code or other text editor, you need to install nodemon. For that, you need to install globally with keyword -g
npm install -g nodemon 


  • To access in console bar with nodemon, write this in terminal
    nodemon .\demo.js
  • If  You see the error while running nodemon, Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system, follow this link

Simple Test and Async Test with Jasmine

npm install --save-dev jasmine

./node_modules/.bin/jasmine init

npm install --save-dev request

Sunday, March 15, 2020

Get Local IP In node.js

'use strict';

var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
  var alias = 0;

  ifaces[ifname].forEach(function (iface) {
    if ('IPv4' !== iface.family || iface.internal !== false) {
      // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
      return;
    }

    if (alias >= 1) {
      // this single interface has multiple ipv4 addresses
      console.log(ifname + ':' + alias, iface.address);
    } else {
      // this interface has only one ipv4 adress
      console.log(ifname, iface.address);
    }
    ++alias;
  });
});

// en0 192.168.1.101
// eth0 10.0.0.101