Node.js
All about Node.js
- Ideal for building real-time applications, APIs, and microservices.
- Written in C++
- Download NodeJs on your OS
- The installer will install
npm
package manager by default - Recommended to install Node Version Manager to easily change between node versions.
- The installer will install
npm
is a package manager that allows install tons of JS libraries from community.
1. Create a Project
- Create a folder
- In this directory, execute
npm init
- A package.json is created
- Every Node project has a package.json providing information such as project's name, author, version, and dependencies.
2. Install dependencies
- Install all the libraries with their specific version that
package.json
specifies
npm i
3. Run Project ✅
- If you want to run your project, already set up. We can use the next command:
node file-name.js
Nodemon (Live Server) 💨
- Another alternative to run our projects, Nodemon
- Nodemon is like Live Server but in Node.js
- Normally, to see our changes in a Nodejs project, we would need to shut down server and start it again
- Nodemon does this automatically, save our lifes!
Install Nodemon:
npm i nodemon
Once installed, add these lines to the "package.json" "scripts"
...
"scripts" : {
//"dev": "nodemon index.js",
"start": "nodemon index.js"
},
...
Note: Create index.js/other_name.js. Here must have your app.listen(PORT)
Now, we can RUN our project with Nodemon
npm run start
Warning
If we are working with template engines, the template engine may not update because nodemon does not know this new template engine extension (We will discuss what is template engines on JS Express). For example: If we are working with .ejs
or .hbs
files, so we must add this.
To fix this:
- Create in root directory:
nodemon.json
- Add a JSON
{
"ext": "js,json,ejs,hbs, <otherFileExtensions>"
}
- Restart server to take effect:
Ctrl+C
thennpm run start
Deprecrated
New hot reloading with --watch
flag. Now, run node index.js --watch
to watch hot reloading. Does not work with Node.ts
Setting PORT 🔨
We usually run our project on a server. For example, Live Server uses PORT 3000 so we can view our project on localhost:3000
.
In NodeJS, we must set up manually. In index.js:
const express = require('express')
const app = express()
const PORT = 3000 // Can use any port number
app.listen(PORT, () => console.log(`Server: http://localhost:${PORT}`))
Node Asyn 🆚 Sync
Node supports both synchronous and asynchronous
- Synchronous Programming
- Code that performs one task after another, waiting for one to complete before starting another
- Asynchronous Programming
- We don't wait for the code to finish, it's async
Event Loop 🔄
Responsible for managing tasks in the queues, and decides which task execute next in the Call Stack.
The Event Loop does the following process:
- Takes a
MacroTask
from the queue and execute - Next, takes a
microTask
from queue and execute- keeps executing
microTasks
until there is none - No
MacroTask
is selected untilmicroTasks
are done
- keeps executing
- Once
microtask
are done, next a newMacroTask
takes in to be process