To run node directly in terminal :
write node -It will create an node environment;
Make folder for node js to provide an environment:
Create Folder-mkdir nodejs
then cd nodejs
then code .
How to run file created in nodejs named indexjs:
in nodejs after cd check ls
the run using node index.js
-About-npm(Node Package Manager)
npm init
then
package name: (nodejs) node-js-crash-course
version: (1.0.0)
description: This is crash course
entry point: (index.js)
test command:
git repository:
keywords: nodejs,javascript
author: Prince Rai <
[email protected]>
select yes
how to install package for dependencies
-npm install cli-color (now if we see under package.json new depencies found)
-Agar hum kisi ko file bhejte hai toh node modules delete kar denge because it is
very large phir wo apne terminal me jyo hi "npm install" karega node modules aa
jayenge
also install dev depandencies as:
npm install -D nodemon
The next feature of nofdejs due to which it is more powerful and popular is
"module-system"
-create module.js file (node js me hr ek file module hoti hai)
how to inport modules?
IIME(Immediately invoked function)with call;
(function(name){
console.log(name)
})('Prince')
Toh module k andar saari cheeje is liye encapsulate hoti hai taki koi bahari use na
kar paye for eg. agar hm var a=10 andar function declare karte hai toh wo function
call k baad khatam ho jayega after that we will not able to use it.
node js module mme in sbko wrapkar deta hai ..
const wrapper=[
'(function(exports,require,module,__filename,__dirname){','\n});'
];
npm package ko load hm 'require' se karte hai in any module.
ex---
const color=require('cli-color')
console.log(color.red('Hello Prince'));
now create another module auth.js:
const register = function(username){
console.log(`User ${username} has been registered`)
}
//to export
module.exports=register
//Local module
// const auth=require('./auth')
// auth.register('Prince')
// auth.login('Prince','*Prince123')
//CORE modules(already built in node js)
go to node js docs select version and then left side are some modules
1.dirname
2.filename
3.extension
4.parse
5.join(most frequently used in node js)
//File System
1.Make a directory(File)
const path=require('path');
const fs=require('fs');
fs.mkdir(path.join(__dirname,'/test'),(err)=>{
if(err){
console.log(err);
return
}
console.log('Folder created...');
})
this will create a folder of name test and if again run node module it will give
error
2.create a File
Three parameters are there
fs.writeFile(path.join(__dirname,'test','text.txt'),'Hello New Year',(err)=>{
if(err){
throw err;
}
console.log('File created...');
})
when we will run it will create file in test folder of name test.js.
It will not thron err if we create file one time
It generally deletes ecxisting file and create new file with its data.
so, we use appendFile to add data in existing File:
fs.appendFile(path.join(__dirname,'test','text.txt'),'More Data',(err) => {
if(err){
throw err;
}
console.log('Data added...')
})
3.Read a File(It gives result as buffer by default)
fs.readFile(path.join(__dirname,'test','text.txt'),(err,data)=>{
if(err){
console.log(err)
return
}
//convert data from binary buffer to UTF
const content=Buffer.from(data)
console.log(content.toString());
})
//2nd method(very easy)
fs.readFile(path.join(__dirname,'test','text.txt'),'utf-8',(err,data)=>{
if(err){
console.log(err)
return
}
console.log(data);
})
//Operating System module: Gives information about the OS on which you are
working,mostly used for applicaton like server monitoring
1.OS type() or platform()
2.cpu architecture -arch()
3.Cpu details -cpus()
4.FreeMemory()-freemem()
5.Total memory=totalmem()
6.uptime(restart of computer in seconds)-uptime()
// Events module(very important in nextjs
maan lijiye koi user ne register kiya phir bahoot kaam hota hai like email
bhejna,database k andar store karna, mailing list k andar use karna......
so we use this module..
1example:
const Emitter=require('events')
const myEmitter=new Emitter()
myEmitter.on('somename',(data)=>{
console.log(data)
)}
myEmitter.emit('somename',{
name:'Rakesh'
})
2 example:
const Auth{
.......see code}
mainly used in projects to call events like sending email authenticating etc
//HTTP module(used to create server )
index file k andar
create server
change scripts so that we not need to run again and again after changes
"start":"node index",
"dev":"nodemon index"
code:
const http=require('http')
const app=http.createServer((req,res)=>{
res.end('<h1>Hello Prince </h1>')
})
const PORT=process.env.PORT|| 3000
app.listen(PORT,()=>{
console.log(`Listening on Port ${PORT}`)
})
now run by npm run dev(here we use) or npm run start
now we create public file index.html to render differently :
first we have to read file by fs module:
then path needed:
then url
content type is very important
json html plain etc are its types
refactoring of code make this code universal
const app=http.createServer((req,res)=>{
res.writeHead('200',{
'Content-Type':'text/html'
})
if(req.url==='/'){
fs.readFile(path.join(__dirname,'public','index.html'),(err,content)=>{
if(err){
throw err
}
res.end(content)
})
}
else if(req.url==='/about'){
fs.readFile(path.join(__dirname,'public','about.html'),(err,content)=>{
if(err){
throw err
}
res.end(content)
})
}
})