express Flashcards

1
Q

How do you add express to your package dependencies?

A

npm install express –save

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 which expects at least one argument, which is port number

aka

app. listen(path, [callback])
- starts a UNIX socket and listens for connections on the given path; identical to Node’s http.Server.listen()

command line alternative to node index.js to start a server: npm start

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

express hello-world example

A
const express = require('express')
const app = express()
const port = 3000

app.get(‘/’, (req, res) => {
res.send(‘Hello World!’)
})

app.listen(port, () => {
console.log(Example app listening at http://localhost:${port})
})

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

const PORT = process.env.PORT || 4000

A

checks your environment variables to see if you already have a PORT defined there if not it will use PORT 4000

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

How do you mount a middleware with an Express application?

A

app.use method

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

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

A

req/res objects

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

express middleware

A

Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls.

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

middleware functions

A

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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
10
Q

json response in express

A

Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().

The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.

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

what’s the difference between res.json and res.send?

A

res.json will convert non objects (ex. null, undefined etc) as well which are actually not a valid JSON whereas res.send will not convert them.

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

what does res.json do?

A

uses JSON.stringify() under the hood to serialize objects into JSON

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

what is parsing middleware?

A

parses incoming requests with JSON payloads

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

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

A

a new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred

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

what is parsing middleware?

A

parses incoming requests with JSON payloads

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

what’s status code 201?

A

The request has been fulfilled and resulted in a new resource being created

17
Q

what’s json replace and json spaces?

A

‘json replacer’ JSON replacer callback, null by default

‘json spaces’ JSON response spaces for formatting, defaults to 2 in development, 0 in production

18
Q

what’s req.body?

A

The req.body property contains key-value pairs of data submitted in the request body. By default, it is undefined and is populated when you use a middleware called body-parsing such as express.urlencoded() or express.json().cd .

19
Q

res.setHeader example

A

res.setHeader(‘Content-Type’, ‘application/json’)

20
Q

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

A

determines what to do w/ the data at a certain path;

i.e., get, post, delete;

communication btwn server & client, where the server “asks” (hence request) using a http method

21
Q

kill a port

A

kill -9 $(lsof -ti:3000)

22
Q

What does express.static() return?

A

a middleware function - serve files from within a given root directory

23
Q

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

A

directory name

24
Q

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

A

joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path