express Flashcards
How do you add express to your package dependencies?
npm install express –save
What Express application method starts the server and binds it to a network PORT?
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
express hello-world example
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}
)
})
const PORT = process.env.PORT || 4000
checks your environment variables to see if you already have a PORT defined there if not it will use PORT 4000
How do you mount a middleware with an Express application?
app.use method
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
req/res objects
express middleware
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.
middleware functions
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.
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
json response in express
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.
what’s the difference between res.json and res.send?
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.
what does res.json do?
uses JSON.stringify() under the hood to serialize objects into JSON
what is parsing middleware?
parses incoming requests with JSON payloads
What does the express.json() middleware do and when would you need it?
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
what is parsing middleware?
parses incoming requests with JSON payloads