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:
- Event based
- Non-Blocking
- 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
- version - Current version
- name - Application name
- description - Brief description about application
- name - Entry point of an application
- scripts - Define set of node scripts that you can run
- dependencies - list of npm packages installed as dependencies
- 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:
- arch()
Return architecture arm, x64, arm64 - cpus()
Return information about cpus - freemem()
Return free memory size in the bytes - homedir()
Return the home directory path for the current user. - networkInterfaces()
Return the information about network interfaces available on the system. - platform()
Return the platform - totalmem()
Return total memory - type()
Return the type of os linux , window - uptime()
Return the number of seconds when the last time computer rebooted. - userInfo()
Return an object that contain username, id, shell, etc.
Node js path module
const path = require("path")
- basename()
Return last portion of the path - dirname()
Return the whole path except the last portion. - extname()
Return the extension part of the path - join()
Join two or more parts of the path.
Node js file system module
- readFile(path, encode, cb(err, data))
Read a file content - writeFile(path, data, (err))
Write new content to the file - appendFile(path, data, (err))
Append new content to the file - unlink(path, (err))
Remove the given path - rename(old_path, new_path, (err))
Rename the old path with new path - mkdir(path, (err))
Create new directory - rmdir(path, (err))
Remove directory