express Flashcards
How do you add express to your package dependencies?
by using npm to install the express package
npm install express
npm i express
What Express application method starts the server and binds it to a network PORT?
- listen() method
How do you mount a middleware with an Express application?
app.use(/filepath)
app.use( )
- app.use()
const express = require(‘express’);
const app = express();
app.use((req, res) => {
// eslint-disable-next-line no-console
console.log(‘req.method: ‘, req.method);
res.send(‘a string’);
});
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
The request and response objects
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
What does the express.json() middleware do and when would you need it?
parses incoming JSON requests and puts the parsed data in req
it is a built-in middleware function in Express. it parases incoming requests with JSON payloads and is based on body-parses
– use it only when you need to
- it will allow app to parse JSON request bodies
- you need it when you’re expecting incoming POST requests
This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option
express middleware than parses raw json string body into an object and attaches it to the body of the request object - need it when you want to use the contents of an application/requestbody
Parses incoming requests with JSON payloads.
It’s needed when trying to parse incoming JSON data.
What is the significance of an HTTP request’s method?
- gives a way to indicate our intention between client/server
- the methods only do the thing they say if WE BUILD THEM to do those things.
It allows the user to properly access the server. If the request method didn’t have the proper functionality, it would be extremely problematic.
It is an action to be performed on a resource identified by a given Request-URL.
What does express.static() return?
a function
a middleware function
serveStatic function
What is the local __dirname variable in a Node.js module?
The directory name of the current module. This is the same as the path.dirname() of the __filename.
It starts with / so it is absolute file path since it starts at the root of the file system
absolute path to the current module.
The directory name of the current module.
What does the join() method of Node’s path module do?
all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
combines multiple strings into a directory path format