Node js
What is Nodejs?
Environment to run javascript out of browser
Built on top of chrome's V8 JS Engine (fastest and most efficient as of now)
Full-stack and big community
Familiarity required for understaing?
HTML, CSS, JS, ES6
Callbacks, Promises, Async-Await
Nodejs
Properties
No DOM
No Window
Server Side Apps (pure logic without UI or graphics)
File System
Versions (no browser dependence)
CommonJS
Global - (No Window in here)
__dirname - path to current directory
__filename - file name
require - function to use modules (NodeJS)
module - info about current module (file)
process - info about env where the program is being executed
console -
setTimeout and setInterval
Modules in node
Create multiple modules to keep the app clean and seggregated
module.exports
Built-in modules
OS (for interaction with OS and server) - require('os')
os.userInfo()
os.uptime()
os.type()
os.totalmem()
os.freemem()
PATH - require('path')
path.join(a1, a2, a3)
path.basename("some path")
path.resolve(a1,a2,a3)
FS - require('fs')
fs.readFileSync("file path", encoding)
fs.writeFileSync("file path", "content to write")
if file is not there, create one
if file is already there, will overwrite
for appending, path one more argument: {flag: 'a'}
readFile and writeFile are also possible, with callback functions
HTTP - require('http')
http.createServer((req, res) => {})
NPM (Node Package Manager)
what is it?
Commands:
npm --version
npm i <package name> (i to install a node package, locally installed)
(sudo)? npm i -g <package name> (to install package globally, use in any project)
npm i -D <package name> (-D add package to devDependencies in package.json file)
npm i --save <package name> (--save add package to dependencies in package.json file)
package.json - menifest file, stores important information about the project/package
npm init -y (to create package.json, y flag set everything by default)
Important Topics
Event Loop
Allows node js to perform non-blocking I/O operations (despite JS being single-threaded)
It does so by offloading operations to system kernel whenever required
References:
a
Async Patterns
a
Events Emitter
s
Streams
HTTP Requests and Response Cycle
How web works and data transfer on web?
User / client sends http request, server sends http response
http messages have following parts:
start line
optional header (meta information, key value pairs)
optional body
blank line (to indicate end of data packate)
Request methode part:
method
get
post
delete
update
create
url
http version
Response methode part:
status code
status text
headers
body (optional)
References
Last updated
Was this helpful?