NODE JS (Backend Developer)

Isha Bharti
4 min readFeb 10, 2021
  1. node js=Node.js is an open source server environment.
  2. Node.js allows you to run JavaScript on the server
  3. The Node. js Read-Eval-Print-Loop (REPL) is an interactive shell that processes Node. js expressions. The shell reads JavaScript code the user enters, evaluates the result of interpreting the line of code, prints the result to the user, and loops until the user signals to quit
  4. Module in Node js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node js application. Each module in Node js file under a separate folder.
  5. Making file and write=const fs = require(‘fs’);

fs.writeFileSync(“read.text”,”welcome isha in node js”)

creating file

fs.writeFileSync(“read.text”,”welcome isha in node js”)

// fs.writeFileSync(“read.text”,”isha in node js”)

// add using append

fs.appendFileSync(“read.text”,”\n how are u”)

// to rename the filefs.renameSync(“read.text”,”read_write.txt”)

5. CREATING FOLDER IN NODE JS=

const fs = require(‘fs’);

const path = require(‘path’);

fs.mkdir(path.join(__dirname, ‘file’), (err) => {

if (err) {

return console.error(err);

}

console.log(‘Directory created successfully!’);

});

8.How to remove file =fs.unlinkSync(“done.txt”)

9. Buffer data =If data is coming in buffer so i have to take another variable and assign the variable name after that i have to write= tostring() .

10.Asynchronous =its taking multiple request in one time exapmle= it will work in multiple way.

11.USING ASYC and making file in node=creating file

const fs= require (‘fs’)

fs.writeFile(“isha.txt”,”hey girls”,(err) =>{

console.log(“created suceesfully”)

})

reading file

const fs= require (‘fs’)

fs.readFile(“isha.txt”,”UTF-8",(err,data) =>{

console.log(data)

})

utf-8 we are writing becuase of buffering if we will not write then it will come in buffering data type.

Deleting file

const fs= require (‘fs’)

fs.unlink(“isha.txt”,(err) =>{

console.log(“ok”)

})

12.Node.JS OS Module To Get Operating System =The os module provides operating system-related utility methods and properties. in os we can check architecture using

os arch =Returns the operating system CPU architecture for which the Node.js binary was compiled. Possible values are 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'.

const os= require (‘os’)

console.log(os.arch())

we can see host name also-

const os= require (‘os’)

console.log(os.hostname())

we can check memory =

os.freemem()

Returns the amount of free system memisha@123

ry in bytes as an integer.

const os= require (‘os’)

const freeMemory=os.freemem();

console.log(`${freeMemory/1024/1024/1024}`)

we can check total memory also

const os= require (‘os’)

const totalMemory=os.totalmem();

console.log(`${totalMemory/1024/1024/1024}`)

FOR homedirectory

const os= require (‘os’)

console.log(os.homedir())

os.cpus()

Returns an array of objects containing information about each logical CPU core.

const os= require (‘os’)

console.log(os.cpus())

os.endianness()

Returns a string identifying the endianness of the CPU for which the Node.js binary was compiled.

const os= require (‘os’)

console.log(os.endianness())

os.platform()

Returns a string identifying the operating system platform. The value is set at compile time. Possible values are 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.

const os= require (‘os’)

console.log(os.platform())

os.type()

Returns the operating system name as returned by uname(3). For example, it returns 'Linux' on Linux, 'Darwin' on macOS, and 'Windows_NT' on Windows.

const os= require (‘os’)

console.log(os.type())

Path

The path module provides utilities for working with file and directory paths. It can be accessed using:

const path= require (‘path’)

console.log(path)

13.How to create and export own modules.

making two js file

in first file =

const add = (a,b) => {

return a+b;

};

module.exports(sub)

in second file=

const add = require(“./oper”);

console.log(add(4,5));

with multiple function

1.const {add,sub}= require(“./oper”);

console.log(add(4,5));

console.log(sub(19,5));

2.const add = (a,b) => {

return a+b;

};

const sub = (a,b) => {

return a-b;

};

const name=”vindo”;

module.exports.add=(add);

module.exports.sub=(sub);

13.What is npm?

npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently. … Most commonly, it is used to publish, discover, install, and develop node programs. Run npm help to get a list of available commands.

chalk -we are using chalk for giving colour ,highlight,changing and we can change the font also.

$ npm install chalkconst chalk = require('chalk');console.log(chalk.blue('Hello world!')

validator

This library validates and sanitizes strings only.

If you’re not sure if your input is a string, coerce it using input + ''. Passing anything other than a string is an error.

FIRSTLY WE HAVE TO INSTALL VALIDATOR

const validator=require(“validator”)

const res=validator.isEmail(“ishagmail.com”)

console.log(res)

14.Import global module

nodemon

nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

15.WHAT IS WEB SERVER?

= A web server is a computer that runs websites. It’s a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users. This intercommunication is done using Hypertext Transfer Protocol (HTTP). These web pages are mostly static content that includes HTML documents, images, style sheets, test etc. Apart from HTTP, a web server also supports SMTP (Simple Mail transfer Protocol) and FTP (File Transfer Protocol) protocol for emailing and for file transfer and storage.

Node.js Web Server

Create Node.js Web Server

const http = require(“http”);

const server=http.createServer((req,res)=>{

res.end(“hello from my side”);

});

server.listen(8000,() =>{

console.log(“listeningn to the port no 8000”);

});

Json (Java script object notation)

It is storing and formatting Data,its lightweighet

store data and print it

bio_data= {

name:”isha”,

age:21,

sex:”Female”,

country:”indian”

}

console.log(bio_data)

change in json = using json stringify

bio_data= {

name:”isha”,

age:21,

sex:”Female”,

country:”indian”

}

const data=JSON.stringify(bio_data)

console.log(data)

changing json to object = using json parse

bio_data= {

name:”isha”,

age:21,

sex:”Female”,

country:”indian”

}

const data=JSON.stringify(bio_data)

const obj=JSON.parse(data)

console.log(obj)

Making file in json

const fs=require(“fs”)

bio_data= {

name:”isha”,

age:21,

sex:”Female”,

country:”indian”

}

const data=JSON.stringify(bio_data);

fs.writeFile(“name.json”,data,(err)=>{

console.log(“done”);

});

--

--