NODE JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Nodejs is javascript engine from Chrome V8 Engine . | |
Which runs javascript code in the backend | |
node js is non blocking io module ie., if any lines takes more time , than the cpu does not block other lines and continues execution of other lines | |
npm - package manager for js .Similar to pip in python | |
Note: Never push node module dependencies to Git as it is huge | |
docs: all functions in npm is | |
- https://nodejs.org/dist/latest-v17.x/docs/api/ | |
- https://nodejs.dev/learn/introduction-to-nodejs | |
*/ | |
//ubuntu Linux Install | |
sudo apt update | |
sudo apt install nodejs | |
node -v // --version | |
sudo apt install npm | |
//Run Node | |
node file.js //execute file | |
node //start nodejs shell | |
//package create and install | |
npm init //create a project using npm | |
npm install express --save | |
npm install -g package_name //install globally | |
5 //5 | |
5+5 //10 | |
console.log("hi") //print | |
console.log(__filename) // | |
//variable | |
var a=1 | |
const c =1 | |
//Map | |
const map1 = new Map(); | |
map1.set('a', 1); | |
console.log(map1.get("a")) | |
//List | |
var a = [1, "two", "three", 4]; | |
if (a[0] == 1) | |
console.log("equal") | |
else | |
console.log("not") | |
//Loop | |
for (i = 0; i <= a.length - 1; i++) console.log(i) | |
a.forEach(i => console.log(i)) | |
//function | |
function f(name){console.log(name)} //f("hi") | |
//IMPORT OTHER FILE | |
//library.js | |
d = { "name": "deepak", "age": 100 } | |
module.exports = { add, d} | |
function add(x, y) { return x + y;} | |
//index.js | |
const lib = require("./library.js") | |
console.log(lib.add(4, 2), lib.d); | |
//common js vs ESM | |
const library = require("library") //commonjs | |
import * from library as lib //ESM - only work when you convert the current project to module ( "type": "module") | |
//IMPORT DEPENDENCY | |
const dep = require("os") | |
console.log(os.freemem()) | |
//SYNC VS ASYN LIBRARIES | |
const fs = require("fs") | |
fs.writeFileSync("f.txt","data to be written") | |
fs.writeFile("f.txt","data to be written", () =>{console.log("done writing")}) // intimate user once data is written | |
//IMPORTATN LIBRARIES | |
path //folder path | |
fs //file | |
url | |
events | |
express //to create server alternate to "http" package | |
//EVENTS | |
const event = require('events'); | |
class E extends event {} | |
const e = new E(); | |
e.on('WaterFull', () => { | |
console.log('Please turn off the motor!'); | |
setTimeout(() => { | |
console.log('Please turn off the motor! Its a gentle reminder'); | |
}, 3000); | |
}); | |
console.log("started") | |
e.emit('WaterFull'); //start event when you receive "Waterfull" | |
console.log("The script is still running") | |
// Result | |
started | |
Please turn off the motor! | |
The script is still running | |
Please turn off the motor! Its a gentle reminder | |
//EXPRESS - https://expressjs.com/en/starter/hello-world.html | |
const express = require('express') | |
const path = require('path') | |
const app = express() | |
const port = 3000 | |
//http://localhost:3000/home/hi?id=5 | |
app.get('/home/:param', (req, res) => { | |
res.send('Hello World!' + req.params.param + req.query.id) | |
}) | |
app.get('/file', (req, res) => { | |
res.sendFile(path.join(__dirname, "file.html")) | |
}) | |
app.post('/file', function(req, res) { | |
res.send(("Working")); | |
// console.log('req.body.name', req.body['submit']); | |
}); | |
app.get('/json', (req, res) => { | |
res.json({ "name": "deepak" }) | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) |