1/31
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is NodeJS
A runtime environment that allows users to run javascript outside the browser, mainly on servers.
Used to build fast, scalable network applications.
Ideal for APIs, real-time apps and servers.
Why NodeJS is fast and efficient
It uses an event-driven, non-blocking IO model that makes it lightweight and efficient
Event-driven
Reacts to requests, file reads, timeouts
Non-blocking I/O
I/O doesn’t wait for slow operations to finish.
Lightweight and efficient
Handles many users with fewer resources
Features of NodeJS
Asynchronous and event-driven
Very fast
Single threaded
No buffering
Asynchronous and event driven
Operations like file reads, timers and database queries run asynchronously.
Very fast
Uses the V8 engine, which translates JavaScript code to machine code.
Minimal overhead for handling requests.
Single-threaded
One main thread
Each request is stacked on top of each other instead of generating a new thread per request.
This makes it memory-efficient and scalable
No buffering
Data is processed in streams
Reduces memory usage
What is NodeJS used for
Generate dynamic page content
CRUD files on server
Collect form data
Build REST APIs
How to create server using NodeJS
Import required modules
Start server
Add listener
Import required modules
const http = require(‘http‘);
const port = process.env.PORT || 3000;
Start server
const server = http.createServer((res, req)=> {
res.writeHead(200, “Content-type“: “text/plain“);
res.end('Hello world!')
});
Add listener
server.listen(port);
What is express
Web application framework for NodeJS
Acts as middleware between client and server, handling data flow between them.
Why is Express good
Structure: provides a standard way to organise web applications.
Productivity: removes boilerplate (like manual URL parsing)
Tools: offers pre-built libraries for handling requests, routing and connecting to databases.
Features of Express
Allows to set up middleware to respond to HTTP requests.
Allows to dynamically render HTML pages based on passing arguments to template.
Define a routing table which is used to perform different actions based on an HTTP method and URL.
Basic express application
const express = require('express');
const app = express;
app.get(‘/‘, (res, req) => {
res.send(<h1>Home Page</1>“);
});
listen(3000);Middleware function
A function that runs between the request and the response.
They have acces to:
req
res
next
What middleware functions do
Execute any code
Modify the request and response object
End request-response cycle
Call the next middleware function in the stack.
Types of middleware functions
Route handler
Logging middleware
Error handler
Route handler
app.get(‘/path‘, (req, res) => {
res.send(“Home Page“)
});Logger Middleware
app.use((req, res, next) => {
console.log("Log something");
next();
});Error handler
app.use((err, req, res, next) => {
console.log(err.stack);
res.status(500).send("Something Broke");
})Request Object
Represents the HTTP request
Contains information about the body, headers, parameters
Request Object methods
req.body
req.headers
req.params
req.query
Response Object
Represents the HTTP response that the Express app sends when it receives a request.
Response Object methods
res.send(body)
res.json(json)
res.status(code)
res.redirect(path)
res.render(view, locals)
Express Router
A mini version of an Express application that handles routes, has its own middleware and can be exported and reused.
Routers break down the application into mini-apps
Express body parsing
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended=false}));
app.post('/submit', (req, res) => {
console.log(req.body) // form data is here
res.redirect(/);
});Express serving static files
const path = require('path');
app.use(express.static('folder'));