NodeJS
Node.js
It’s an asynchronous event driven JavaScript Runtime. it’s designed to build scalable network applications.
- it’s actually not a framework, instead it’s a runtime environment. it enables us to use javescript on a computer and not just limited to wihtin the browser.
Express, on the other hand is actually a javascript framework that allow us to create backend for our websties.
fs module
introduction
In JavaScript, the term “fs” typically refers to the “File System” module. The “fs” module is a built-in module in Node.js, which is a JavaScript runtime environment used for server-side applications.
The “fs” module provides methods and functionality to interact with the file system, allowing you to read from and write to files, create directories, modify file permissions, and perform other file-related operations. It enables you to work with files and directories on your computer or server.
Here’s an example of how you can use the “fs” module in Node.js to read the contents of a file:
1 | javascriptCopy codeconst fs = require('fs'); |
In the above example, fs.readFile
is used to read the contents of the file named “file.txt” in the current directory. The callback function receives any potential error (err
) and the file contents (data
). If no error occurs, the contents of the file are printed to the console.
Note that the “fs” module is specific to Node.js and is not available in web browsers, as JavaScript running in a browser has limited access to the local file system for security reasons.
Synchronous vs. Asynchronous
Node.JS only has one thread and all the users accessing your application are using this thread.
Synchronous code is also called blocking code because a certain operation can only be executed after the one before has finished.
eg. readFileSync
Asynchronous code is non-blocking code. Asynchronous code is a programming paradigm that allows multiple tasks to be executed concurrently without blocking the execution of the program. While one task is being processed asynchronously, the program can execute other tasks or respond to user input, improving responsiveness and overall performance.
eg.readFile
in synchronous, if one user locks the single thread, all the other users need to wait.
in asynchronous, it does the heavy work in the background, and once the data is available, the call back function will get called in the single thread to access the single data. At the same time all the users can perform their tasks one after another in the single thread.
callback hell
Callback hell is a situation that arises in asynchronous programming when multiple nested callbacks are used, leading to complex and unreadable code. It occurs when one asynchronous operation depends on the result of another, resulting in deeply nested callback functions.
NPM
install npm command: npm init (type in the terminal opened in the project)
after installing successfully, there will be a file called package.json
1 | npm init |
when we ran this command, we install the package locally so it mean it can only work in this project.
But if you install a package globally then you don’t need to install that package each time you have a new project.
1 | // install globally: |
use nodemon ( globally):
1 | nodemon index.js |
require 3rd party module
1 | const slugify = require('slugify'); |
package version
1 | "devDependencies": { |
node_modules folder:
it has all the dependencies.
there’s no need to share this folder when you share your code with other people because it’s very big.
when we download someone’s node without this folder, how do we install those dependencies ourselfves
1 | npm install |
package-lock.json file: it has all the versions and names and details of the packages that we are using.
when you share your code or you use other people’s code, you want all the versions of those packages to be the same so the code works the same for the everyone. So make sure you always share both package.json file and package-lock.json
note
- enter node repo:
XIANGDANTONG@EGG ~ ❯❯❯ node
Welcome to Node.js v16.15.0.
Type “.help” for more information.
fs
file system
1 | //require module |
npm (node pacakge manager
1 | // it brings up the initialization utility, create configuration file which is called the package.json |
- install npm package:
npm i packagename1 name2 name3 // i is short for install
- string interpolation:
1 | var generateName = require("sillyname"); |
CJS ->common JS
ESM ->ecmascript module
another way to require module
CJS or ESM you can only choose one in the project
1 | package.json: |
Express
a javascript framework.
- add module:
1 | npm install express |
creating a server:
1
2
3
4
5
6
7
8//require express module
const express = require("express");
const app = express();
// listeing on port 3000
app.listen(3000, function () {
console.log("listening on port 3000");
});routes:
1 | //require express module |
TEMPLATE
Make a new folder
Inside the folder, create a new file called sth.js
Set up a new NPM package npm init -y
Using NPM install the express module
1
2
3
4
5
6
7
8
9
10
11
12
13npm install express
//require express module
const express = require("express");
const app = express();
app.get("/about", function (req, res) {
res.send("call me maybe");
});
// listeing on port 3000
app.listen(3000, function () {
console.log("listening on port 3000");
});Require express in your calculator.js
Setup express
Create a root route get method with app.get()
Body Parser:
allow us to pass the information that we get sent from post request.
- install:
npm i body-parser
caculator project:
js:
1 | const express = require("express"); |
html:
1 | <!DOCTYPE html> |
BMI Caculator
1 | app.get("/bmicalculator", function (req, res) { |