Lectures List
capscom technology computer course almora

Node JS Tutorial with all the important packages

NODE JS 

It is an open source and cross platform, Node JS written in pure java script and it can be run in java script environment.
Now Java script run in client side as well as server side.

Features:

  1. Event based
  2. Non-Blocking
  3. Asynchronous I/O Framework

Uses:

Google's V8 Engine

Install Node JS.

HTTP Module

This module does not require any additional resources.
It is used to create http server.

Method:

createServer( (req, res)=>{	
} ).listen(port);

Save:

first.js

Response Methods:

writeHead(200, {
'Content-Type':'text/plain'
});
Tells to the browser everything is OK and data is in plain text.

.write(text)

Text to the body.

.end()

Tells to the server that all of the headers and body have been sent

Port environment variable

process.env.PORT

Access this offline while debugging

.listen(process.env.PORT || 3000);
 
Where 3000 will be used for offline port number.

Node JS Routing

req.url

Restart application automatically

Package

nodemon

Install

npm install -g nodemon

Run using nodemon

nodemon file_name.js

Access environment variable

  • Create .env file at root
  • use dotenv package
  • npm install dotenv

Load dotenv

require("dotenv").config();

Access .env variables

console.log(process.env.WEBSITE_NAME);

Output to the command line using node js

Format specifiers
  • %s - String
  • %d - Number
  • %i - Integer part only
  • %o - Object

Methods

  • .log() - To print string
  • .clear() - To clear console

Import Export node js file

Export
module.exports = identifier

Export as property of exports

let name="CAPSOCM"
exports.name=name;
 
let {name} = require(file_name)
console.log(name)

NPM Node Package Manager

Installing a package

npm install package_name

Updating packages

npm update

Running tasks

{
"scripts":{
"start":"node app.js"
}
}
npm run [task_name]

Package.json 

properties
  1. version - Current version
  2. name - Application name
  3. description - Brief description about application
  4. name - Entry point of an application
  5. scripts - Define set of node scripts that you can run
  6. dependencies - list of npm packages installed as dependencies
  7. devDependencies - list of npm packages installed as development dependecies
npm install <PACKAGE_NAME> --save-dev

uninstall node packages

npm uninstall <package_name>
npm uninstall -g <package_name>

 

Node js os module

const os = require("os")

Methods:

  1. arch() 
    Return architecture arm, x64, arm64
  2. cpus()
    Return information about cpus
  3. freemem()
    Return free memory size in the bytes
  4. homedir()
    Return the home directory path for the current user.
  5. networkInterfaces()
    Return the information about network interfaces available on the system.
  6. platform()
    Return the platform
  7. totalmem()
    Return total memory
  8. type()
    Return the type of os linux , window 
  9. uptime()
    Return the number of seconds when the last time computer rebooted.
  10. userInfo()
    Return an object that contain username, id, shell, etc.

Node js path module

const path = require("path")
  1. basename()
    Return last portion of the path
  2. dirname()
    Return the whole path except the last portion.
  3. extname()
    Return the extension part of the path
  4. join()
    Join two or more parts of the path.

Node js file system module

  1. readFile(path, encode, cb(err, data))
    Read a file content
  2. writeFile(path, data, (err))
    Write new content to the file
  3. appendFile(path, data, (err))
    Append new content to the file
  4. unlink(path, (err))
    Remove the given path
  5. rename(old_path, new_path, (err))
    Rename the old path with new path
  6. mkdir(path, (err))
    Create new directory
  7. rmdir(path, (err))
    Remove directory
Author/Written by: Mr Hemant Singh (Founder)
Terms & Conditions CAPSCOM TECHNOLOGY Pvt. Ltd. का बेहतर रूप से संचालन करने के लिए यहॉ कुछ नियमें और शर्ते लागू है अगर आप कैप्सकॉम टैक्नोलॉजी में पढ़ रहे है तो यह आप पर लागू होता है।