17. Nodejs
Nodejs
- Javascript outside of the browser
- Nodejs is a Javascript Runtime built on Google's OpenSource V8 Javascript Engine.
- Why? To access File System, Better Networking capabilities, built webservers in javascript which are fast, scalable.
- Nodejs is single threaded, based on event-driven, non-blocking I/O Model, which makes us built fast and highly scalable data-intensive applications.
- JavaScript across entire stack.
- NPM: highe library of opensource packages for free
- Use Case:
- Building Servers-side Web Applications
- Builiding APIs with database behind it.
- Data Streaming
- Real Time chat application(Web Sockets)
- Don't use Node
- Application with heavy Server-Side Processing (CPU-intensive) like headvy image-preocessing, video-conversion, file-compression.
- Use Ruby on Rails, PHP or Python for it.
- Error-first callbacks in Node
- using fs Module to read and Write Files Synchronously or Asynchronously.
- Callback Hell, solutions: Promises and Async Await.
- http createServer, listen on it.
- Routing
- res.end()
- res.writeHead(statusCode, moreHeaderObj)
- HTML Templates TODO
Express
MongoDB
Mongoose
Authentication and Authorization
Webdev summary -
deploy it on the internet, there are a few ways -
- Go to aws, GCP, Azure, Cloudflare
- Rent a VM (Virtual Machine) and deploy your app
- Put it in an Auto scaling group
- Deploy it in a Kubernetes cluster
There are a few downsides to doing this -
- Taking care of how/when to scale
- Base cost even if no one is visiting your website
- Monitoring various servers to make sure no server is down
Serverless Backend
- "Serverless" is a backend deployment in which the cloud provider dynamically manages the allocation and provisioning of servers. The term "serverless" doesn't mean there are no servers involved. Instead, it means that developers and operators do not have to worry about the servers.
- There are many famous backend serverless providers - - AWS Lambda - Google Cloud Functions - Cloudflare Workers
- Cloudflare (doesn't use node.js runtime, implements its own, via isolates), wrangler(cloudflare CLI) Hono,js
Monorepos
- npm workspace, Turborepo , Lerna.
- Turborepo
- packages/ui
- apps/docs
- apps/web
process managers
- to keep our server running inifinity, even if it goes down, pms will bring them up again.
Publishing packages
npm login
npm pack
npm publish
Tilde: Flexible Patch
Caret: Flexible Minor and Patch
webpack
entry
loader
- added in modules.rules array as an object {test:
<regex>
, use:[<loaders here>
]} - css-loader, style-loader, babel-loader, svg-inline-loader, sass-loader
- "Hey webpack compiler, when you come across a path that resolves to a '.css/.js/.ts/.svg' file inside of a require()/import statement, use the raw-loader to transform it before you add it to the bundle."
- added in modules.rules array as an object {test:
plugins
- added in plugins array.
- While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.
- comes into play after bundle is created
- HtmlWebpackPlugin - genereats a .html file and add to outdir that references newly created bundler.js
- EnvironmentPlugin(comes with webpack only) - to declare env for app to be dev,prod,stag...
output
mode
[name].[contenthash].js
-> generates dynamic md5 hashed values bundle, withwebpackChunkName: name
file, if bundle content changes b/w builds(for browser caching).
rrd
- useParams()
- Outlet
- https://ui.dev/react-router-tutorial
- rrdv6: the order of the children Routes doesn't matter since Routes is intelligent.