Node JS
QUICK START TRAINING
Table of Content:
● About Node Js
● How to install Node JS
● About NPM
● npm init
● Node Scheduler and Http Request
● Socket.io
● Express Js Framework
● Monitor Live status of APP thorough PM2
● Deployment
About Node Js:-
● Node.js is an open source server environment
● Node.js is free
● Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
● Node.js uses JavaScript on the server
● Node.js eliminates the waiting, and simply continues with the next request.
● Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory
efficient.
● Node.js can generate dynamic page content
● Node.js can create, open, read, write, delete, and close files on the server
● Node.js can collect form data Node.js can add, delete, modify data in your database
How to install Node JS:-
1. Open the official page for Node.js downloads and download Node.js for Windows by clicking the
"Windows Installer" option
2. Run the downloaded Node.js .msi Installer - including accepting the license, selecting the
destination, and authenticating for the install (This requires Administrator privileges, and you may
need to authenticate)
3. To ensure Node.js has been installed, run node -v in your terminal - you should get something like
v6.9.5.
4. Congratulations - you've now got Node.js installed, and are ready to start building!
5. As the final step in getting Node.js installed, we'll update your version of npm - the package manager
that comes bundled with Node.js. npm install npm --global // Update the `npm` CLI client
On Ubuntu:-
sudo apt install nodejs
npm -v or npm –version
About NPM:-
What is NPM?NPM is a package manager for Node.js packages, or modules if you like. www.npmjs.com
hosts thousands of free packages to download and use. The NPM program is installed on your computer
when you install Node.js
What is a Package? A package in Node.js contains all the files you need for a module. Modules are
JavaScript libraries you can include in your project.
Download a Package
Downloading a package is very easy. Open the command line interface and tell NPM to download the
package you want. I want to download a package called "upper-case":
C:UsersYour Name>npm install upper-case
Command:npm init
package name: (mycode) testpackage-ashish
version: (1.0.0) 1.0.0
description: Project Description
entry point: (index.js) app.js
test command:
git repository: git@gitlab.com:ashishinvetech/testpackage-ashish.git
keywords: testpackage-ashish
author: Ashish Gupta
license: (ISC)
Is this OK? (yes) yes
npm login
npm publish
npm publish --access public
How to Initialize nodejs app and publish own module
● Http Request
var http = require('https');
var optionsgetmsg = {
hostname: 'portal.hpsldc.com',
port: 443,
path: '/app/api/index.php/importrtmiexftp',
method: 'GET'
};
var reqGet = http.request(optionsgetmsg, function (res) {
res.on('data', function (d) {
myData += d.toString();
});
res.on('end', function (d) {
console.log(myData);
});
});
reqGet.end();
reqGet.on('error', function (e) {
console.error(e);
});
● Node Scheduler and Http Request
Install node-cron using npm:
$ npm install --save node-cron
Import node-cron and schedule a task:
var cron = require('node-cron')
cron.schedule('* * * * *', () => {
console.log('running a task every
minute');
});
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
● Express JS
Fast, unopinionated, minimalist web framework for Node.js
$ npm install express --save
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
res.sendFile(__dirname + '/index.html');
})
app.post('/', function (req, res) {
res.send('Got a POST request')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Serving static files in Express
app.use('/static', express.static('public'))
http://localhost:3000/static/images/kitten.jpg
The app.locals object has properties that are local
variables within the application.
● Socket.io
npm install socket.io
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log("some one connected");
socket.on('chat message', (msg) => {
console.log(msg);
io.emit('chat message', msg);
});
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
More Detail:-https://socket.io/get-started/
● PM2
1. ADVANCED, PRODUCTION PROCESS MANAGER FOR NODE.JS
2. PM2 is a daemon process manager that will help you manage and keep your application online 24/7
npm install pm2@latest -g
pm2 start app.js
pm2 monit
pm2 monit
pm2 list
pm2 stop
pm2 restart
pm2 delete
Ref:https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
● Deployment
1. Git Clone /pull in server directory
2. npm install to install all dependency
3. pm2 start startpage.js
For multiple virtual host
https://www.digitalocean.com/community/questions/setting-up-multiple-nodejs-applications-using-
nginx-vitual-hosts
Thank You

Node js training (1)

  • 1.
  • 2.
    Table of Content: ●About Node Js ● How to install Node JS ● About NPM ● npm init ● Node Scheduler and Http Request ● Socket.io ● Express Js Framework ● Monitor Live status of APP thorough PM2 ● Deployment
  • 3.
    About Node Js:- ●Node.js is an open source server environment ● Node.js is free ● Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) ● Node.js uses JavaScript on the server ● Node.js eliminates the waiting, and simply continues with the next request. ● Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient. ● Node.js can generate dynamic page content ● Node.js can create, open, read, write, delete, and close files on the server ● Node.js can collect form data Node.js can add, delete, modify data in your database
  • 4.
    How to installNode JS:- 1. Open the official page for Node.js downloads and download Node.js for Windows by clicking the "Windows Installer" option 2. Run the downloaded Node.js .msi Installer - including accepting the license, selecting the destination, and authenticating for the install (This requires Administrator privileges, and you may need to authenticate) 3. To ensure Node.js has been installed, run node -v in your terminal - you should get something like v6.9.5. 4. Congratulations - you've now got Node.js installed, and are ready to start building! 5. As the final step in getting Node.js installed, we'll update your version of npm - the package manager that comes bundled with Node.js. npm install npm --global // Update the `npm` CLI client On Ubuntu:- sudo apt install nodejs npm -v or npm –version
  • 5.
    About NPM:- What isNPM?NPM is a package manager for Node.js packages, or modules if you like. www.npmjs.com hosts thousands of free packages to download and use. The NPM program is installed on your computer when you install Node.js What is a Package? A package in Node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project. Download a Package Downloading a package is very easy. Open the command line interface and tell NPM to download the package you want. I want to download a package called "upper-case": C:UsersYour Name>npm install upper-case
  • 6.
    Command:npm init package name:(mycode) testpackage-ashish version: (1.0.0) 1.0.0 description: Project Description entry point: (index.js) app.js test command: git repository: [email protected]:ashishinvetech/testpackage-ashish.git keywords: testpackage-ashish author: Ashish Gupta license: (ISC) Is this OK? (yes) yes npm login npm publish npm publish --access public How to Initialize nodejs app and publish own module
  • 7.
    ● Http Request varhttp = require('https'); var optionsgetmsg = { hostname: 'portal.hpsldc.com', port: 443, path: '/app/api/index.php/importrtmiexftp', method: 'GET' }; var reqGet = http.request(optionsgetmsg, function (res) { res.on('data', function (d) { myData += d.toString(); }); res.on('end', function (d) { console.log(myData); }); }); reqGet.end(); reqGet.on('error', function (e) { console.error(e); });
  • 8.
    ● Node Schedulerand Http Request Install node-cron using npm: $ npm install --save node-cron Import node-cron and schedule a task: var cron = require('node-cron') cron.schedule('* * * * *', () => { console.log('running a task every minute'); }); # ┌────────────── second (optional) # │ ┌──────────── minute # │ │ ┌────────── hour # │ │ │ ┌──────── day of month # │ │ │ │ ┌────── month # │ │ │ │ │ ┌──── day of week # │ │ │ │ │ │ # │ │ │ │ │ │ # * * * * * *
  • 9.
    ● Express JS Fast,unopinionated, minimalist web framework for Node.js $ npm install express --save const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') res.sendFile(__dirname + '/index.html'); }) app.post('/', function (req, res) { res.send('Got a POST request') }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) }) Serving static files in Express app.use('/static', express.static('public')) http://localhost:3000/static/images/kitten.jpg The app.locals object has properties that are local variables within the application.
  • 10.
    ● Socket.io npm installsocket.io var app = require('express')(); var http = require('http').createServer(app); var io = require('socket.io')(http); app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }); io.on('connection', (socket) => { console.log("some one connected"); socket.on('chat message', (msg) => { console.log(msg); io.emit('chat message', msg); }); }); http.listen(3000, () => { console.log('listening on *:3000'); }); More Detail:-https://socket.io/get-started/
  • 11.
    ● PM2 1. ADVANCED,PRODUCTION PROCESS MANAGER FOR NODE.JS 2. PM2 is a daemon process manager that will help you manage and keep your application online 24/7 npm install pm2@latest -g pm2 start app.js pm2 monit pm2 monit pm2 list pm2 stop pm2 restart pm2 delete Ref:https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
  • 12.
    ● Deployment 1. GitClone /pull in server directory 2. npm install to install all dependency 3. pm2 start startpage.js For multiple virtual host https://www.digitalocean.com/community/questions/setting-up-multiple-nodejs-applications-using- nginx-vitual-hosts
  • 13.