express Flashcards

1
Q

How do you add express to your package dependencies?

A

by using npm to install the express package
npm install express
npm i express

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What Express application method starts the server and binds it to a network PORT?

A
  • listen() method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you mount a middleware with an Express application?

A

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’);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?

A

The request and response objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?

A

application/json

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the express.json() middleware do and when would you need it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the significance of an HTTP request’s method?

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does express.static() return?

A

a function

a middleware function

serveStatic function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the local __dirname variable in a Node.js module?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the join() method of Node’s path module do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly