What is NodeJS
- Runtime enviroment
- Backend
- CLI
- Drone
- Could be said to be wrapper around js engine as primarily objective, tho there are many more uses of it.
Global object := windows
Browser features := webAPI
Can use document
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
- CommonJS -> (Old way) -> NodeJS(Default) => require()
- ESmodule -> (Mew) -> React(Default) => import
What is NPM Node package manager
- File system
- Path
- OS
- http/https
- 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 WorldIn 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));There are two types of modules
- CommonJS => NodeJS
- ESmodule => React
: 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');= 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.