Node.JS & Express

0.0(0)
studied byStudied by 0 people
0.0(0)
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/31

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:58 PM on 1/24/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

32 Terms

1
New cards

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.

2
New cards

Why NodeJS is fast and efficient

It uses an event-driven, non-blocking IO model that makes it lightweight and efficient

3
New cards

Event-driven

Reacts to requests, file reads, timeouts

4
New cards

Non-blocking I/O

I/O doesn’t wait for slow operations to finish.

5
New cards

Lightweight and efficient

Handles many users with fewer resources

6
New cards

Features of NodeJS

  • Asynchronous and event-driven

  • Very fast

  • Single threaded

  • No buffering

7
New cards

Asynchronous and event driven

Operations like file reads, timers and database queries run asynchronously.

8
New cards

Very fast

  • Uses the V8 engine, which translates JavaScript code to machine code.

  • Minimal overhead for handling requests.

9
New cards

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

10
New cards

No buffering

  • Data is processed in streams

  • Reduces memory usage

11
New cards

What is NodeJS used for

  • Generate dynamic page content

  • CRUD files on server

  • Collect form data

  • Build REST APIs

12
New cards

How to create server using NodeJS

  1. Import required modules

  2. Start server

  3. Add listener

13
New cards

Import required modules

const http = require(‘http‘);

const port = process.env.PORT || 3000;

14
New cards

Start server

const server = http.createServer((res, req)=> {

res.writeHead(200, “Content-type“: “text/plain“);

res.end('Hello world!')

});

15
New cards

Add listener

server.listen(port);

16
New cards

What is express

  • Web application framework for NodeJS

  • Acts as middleware between client and server, handling data flow between them.

17
New cards

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.

18
New cards

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.

19
New cards

Basic express application

const express = require('express');
const app = express;

app.get(‘/‘, (res, req) => {
   res.send(<h1>Home Page</1>“);
});

listen(3000);

20
New cards

Middleware function

  • A function that runs between the request and the response.

  • They have acces to:

    • req

    • res

    • next

21
New cards

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.

22
New cards

Types of middleware functions

  • Route handler

  • Logging middleware

  • Error handler

23
New cards

Route handler

app.get(‘/path‘, (req, res) => {
   res.send(“Home Page“)
});

24
New cards

Logger Middleware

app.use((req, res, next) => {
   console.log("Log something");
   next();
});

25
New cards

Error handler

app.use((err, req, res, next) => {
   console.log(err.stack);
   res.status(500).send("Something Broke");
})

26
New cards

Request Object

  • Represents the HTTP request

  • Contains information about the body, headers, parameters

27
New cards

Request Object methods

  • req.body

  • req.headers

  • req.params

  • req.query

28
New cards

Response Object

  • Represents the HTTP response that the Express app sends when it receives a request.

29
New cards

Response Object methods

  • res.send(body)

  • res.json(json)

  • res.status(code)

  • res.redirect(path)

  • res.render(view, locals)

30
New cards

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

31
New cards

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(/);
});

32
New cards

Express serving static files

const path = require('path');
app.use(express.static('folder'));