Skip to content

Latest commit

 

History

History
127 lines (83 loc) · 2.58 KB

File metadata and controls

127 lines (83 loc) · 2.58 KB

Node


What is NodeJS

  1. Runtime enviroment
  2. Backend
  3. CLI
  4. Drone
  • Could be said to be wrapper around js engine as primarily objective, tho there are many more uses of it.

Browser vs NodeJS

Browser

Global object := windows

Browser features := webAPI

Can use document

Node

Global object := global

Can't use document webAPI

Can use all other webAPI

Give access to file system


How many types of module are there := 2

  1. CommonJS -> (Old way) -> NodeJS(Default) => require()
  2. ESmodule -> (Mew) -> React(Default) => import

What is NPM Node package manager

Packages availabe by default in Node

  1. File system
  2. Path
  3. OS
  4. http/https
  5. crypto

require keyword is used to import these Packages

require('./Lecture1/node_intro/app.js')

By default we get empty object as value when we require something


When you need to require the folder then it's must to have a index.js in that folder. It basically is the entry point to the package. Kinda like how it is main() function in C, you import the file in index of package and then export from there so you that is how you do it.

// File: ./myPackage/index.js
module.exports = {
  moduleA: require('./moduleA'),
  moduleB: require('./moduleB')
};

// Now, in another file where you want to use the package...
const myPackage = require('./myPackage');
console.log(`${myPackage.moduleA.myVar} ${myPackage.moduleB.myVar}`);  // prints: Hello World

In this example, index.js is at the root of a package named myPackage that exports all other modules (moduleA and moduleB) from it. This way, you can import the whole package with one line instead of requiring each module separately, keeping your code more readable and organized.


  • You don't have prompt function in node hence you have to provide input using argv only.
let input = console.log(process.argv.slice(2));

Day 3

Require file, folder.

There are two types of modules

  1. CommonJS => NodeJS
  2. ESmodule => React

Day 4

URL

: UNIFIED RESOURCE ALLOCATOR

GET POST PUT PATCH DELETE

console.log(process.cwd());
console.log(__dirname);
  • cwd is there for where you are in and __dirname is there for where the process is running from.
path.join('hello','the','safe','way','to','join','path');

Day 5

Server

= Listen to your request and gives out response.

Node’s HTTP module is enough to build a full server, but it’s low-level and verbose. Express.js sits on top of it and provides routing and middleware abstractions, similar to how chi wraps Go’s net/http.