2 NodeJS
2 NodeJS
“Client-side" programming:
- The code we write gets run in a browser on the user's
(client's) machine
“Server-side” programming:
- The code we write gets run on a server
- Servers are computers run programs to generate web
pages and other web resources
Recall…
When you navigate to a URL:
- Browser creates an HTTP GET request
- Operating system sends the GET request to the server over TCP
When a server computer receives a message:
- The server's operating system sends the message to the server
software (via a socket)
- The server software then parses the message
- The server software creates an HTTP response
- The server OS sends the HTTP response to the client over TCP
Client Server
(Routing,
etc…)
"Server"
NodeJS:
- A JavaScript runtime written in C++
- Can interpret and execute JavaScript
- Includes support for the NodeJS API
NodeJS API:
- A set of JavaScript libraries that are useful for creating
server programs
V8 (from Chrome):
- The JavaScript interpreter ("engine") that NodeJS uses
to interpret, compile, and execute JavaScript code
NodeJS
NodeJS:
- A JavaScript runtime written in C++
Q: What does
- Can interpret and execute JavaScript this mean?
- Includes support for the NodeJS API
NodeJS API:
- A set of JavaScript libraries that are useful for creating
server programs
V8 (from Chrome):
- The JavaScript interpreter ("engine") that NodeJS uses
to interpret, compile, and execute JavaScript code
First: Chrome
Chrome:
- A browser written in C++
- Can interpret and execute JavaScript code
- Includes support for the DOM APIs
DOM APIs:
- JavaScript libraries to interact with a web page
V8:
- The JavaScript interpreter ("engine") that Chrome uses
to interpret, compile, and execute JavaScript code
Chrome, V8, DOM
Parser
JavaScript DOM API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
Parser
JavaScript DOM API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
Parser
JavaScript DOM API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
console.log('V8');
NodeJS, V8, NodeJS APIs
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
const x = 15;
x++;
"Please execute
http
.createServer()"
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
http.createServer();
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
document.querySelector('div');
ReferenceError: document is not defined
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
Parser
JavaScript NodeJS API
Execution runtime Implementation
Engine
(Call stack,
Garbage memory, etc.)
Collector
console.log('nodejs');
NodeJS:
- A JavaScript runtime written in C++
- Can interpret and execute JavaScript
- Includes support for the NodeJS API
NodeJS API:
- A set of JavaScript libraries that are useful for creating
server programs
V8 (from Chrome):
- The JavaScript interpreter ("engine") that NodeJS uses
to interpret, compile, and execute JavaScript code
Installation
NodeJS installation:
- https://nodejs.org/en/download/
- https://github.com/nvm-sh/nvm
- https://github.com/coreybutler/nvm-windows
node command
$ node
> let x = 5;
undefined
> x++
5
> x
6
NodeJS
simple-script.js
function printPoem() {
console.log('Roses are red,');
console.log('Violets are blue,');
console.log('Sugar is sweet,');
console.log('And so are you.');
console.log();
}
printPoem();
printPoem();
node command
$ node simple-script.js
Roses are red,
Violets are blue,
Sugar is sweet,
And so are you.
Server
EventEmitter.on
This server
returns the same
response no
matter what the
request is
Node for servers
app.method(path, handler)
- Specifies how the server should handle HTTP method
requests made to URL/path
- This example is saying:
• When there's a GET request to
http://localhost:3000/hello, respond with the text
"GET hello!"
Handler parameters
https://www.postman.com/
curl
e.g.
JavaScript client
code in a web page:
fetch() to localhost
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Cross-origin solutions
This line of code makes our server now start serving the
files in the 'public' directory directly
Server static data
Example:
https://jsonplaceholder.typicode.com/users/10
Example:
https://jsonplaceholder.typicode.com/posts?userId=1
Content-Type: application/json
{"name": "Bao Bui", "email": "bao@example.com"}
Content-Type: application/x-www-form-urlencoded
name=Bao%20Bui&email=bao%40example.com
POST message body
POST message body
1. Route parameters
2. GET request with query parameters
(DISCOURAGED: POST with query parameters)
3. POST request with message body
Also note that query and route parameters are all strings
Middleware
Middleware
Middlewares
Middleware
*Nguồn: https://iq.opengenus.org/middlewares-in-express/
Middleware
Types of middleware:
− Application-level middleware
− Router-level middleware
− Error handling middleware
− Built-in middleware
− External middleware (requires npm install)
Middleware
Application-level middleware
Middleware
Built-in middleware
- This also allows people who have downloaded your code from
GitHub to install all your dependencies with one command instead
of having to install all dependencies individually
package-lock.json
$ npm i -D nodemon
"scripts": {
"start": "nodemon server.js"
}
https://blog.insiderattack.net/event-loop-and-the-big-picture-nodejs-event-loop-part-1-1cb67a182810
Event loop
https://techva.me/callback-functions-promises-nodejs/
setTimeout
setTimeout(function, delay);
- function will fire after delay milliseconds
JS execution
https://symphony.is/blog/secret-life-event-loop-meetup-overview