Basic Node and Express Flashcards
Create express app object(requiring express module & calling express function)
-npm install express
-const express = require(‘express’);
-const app = express();
Structure for routes in express
app.METHOD(PATH(server-relative),HANDLER);
Types of Methods
GET, POST, PUT, DELETE, OPTIONS, LISTEN, HEAD
What is PATH
A URL pattern or endpoint relative to the server’s domain where the handler function should be executed
What is Handler
A function handling the route when a specific route is matched during incoming HTTP request
What form does the handler take
function(request, response){…}
What do route handlers do?
Provide the core functionality for defining the behavior of your web application’s endpoints
Give example of where route handler goes:
app.get(‘/endpoint’, function(req, res) {
//route handler here
res.send(‘this is the endpoint route’)
});
What are 2 ways to Serve a HTML file:
-express.static middleware
-res.sendFile() METHOD
Show how to serve static files from a directory(e.g ‘public’)
app.use(express.static(‘public’));
When is express.static used
serving multiple static files(e.g HTML, CSS, Javascript)
Show route handler for serving an HTML file with res.sendFile():
app.get(‘/page’, function(req, res){
res.sendFile(__dirname + ‘/public/page.html’);
});
‘/page’ = desired route path
‘/public/page.html’ = file relative to your project directory
When to use res.sendFile()
Useful for serving specific files or for adding additional logic before sending the files
What are middlewares?
-is software logic that intercepts and processes HTTP requests and responses within the application’s request-response cycle. It can modify request and response objects, execute additional code, or terminate the request cycle.
-Are functions that intercept route handlers, adding some kind of information.
-Has access to the request object(‘req’), the response object(‘res’), and the next middleware function in the application’s request-response cycle.
-Can execute code, make changes to the request and response objects, end the request-response cycle, or call the next middlewware function.
what method does middlewares need to be mounted on?
app.use(…);
Show how middleware would be executed for a certain request:
app.use(path, middlewareFunction)
Show how middleware will be executed for all requests:
app.use(middlewareFunction)
Common directory names for static assets:
‘public’, ‘static’, ‘assets’
How to access static assets in the browser, assuming your server is running on port 3000:
http://localhost:3000/index.html
What does JSON stand for?
Javascript Object Notation
Why is JSON commonly used in web development?
-Transmitting data between a server and a web application.
-Easy for machines to parse and generate JSON data.
-Popular due to its simplicity, readability, and flexibility.
What does REST API stand for?
Representational State Transfer Application Programming Interface
What does a REST API do?
Play a crucial role in modern web development by enabling diverse applications to communicate and share data over the web in a flexible, efficient, and straightforward manner.
Serve JSON on a specific route(create route handler that sends JSON data as the response):
app.get(‘/data’, function(req, res){
const jsonData = {
message: “Hello World”,
numbers: [1, 2, 3, 4, 5]
};
res.json(jsonData);
});
What is an .env file?
Configuration file commonly used in Node.js and other server-side applications to manage environment variables