Express.js Flashcards
How do you add express to your package dependencies?
npm install express
What Express application method starts the server and binds it to a network PORT?
listen
How do you mount a middleware with an Express application?
by using the use method of the app object
=> Mounts the specified middleware function or functions at the specified path: the middleware function is executed when the base of the requested path matches path.
app.use(function (req, res, next) {
console.log(‘Time: %d’, Date.now())
next()
})
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
request, response
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?
json() is a built-in middleware function in Express. 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.
=> because the request is not in a json format
What is the significance of an HTTP request’s method?
HTTP defines a set of request methods to indicate the desired action to be performed for a given resource.
its semantic. describes the intent of the logic for communication
What does express.static() return?
middleware 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.
What does the join() method of Node’s path module do?
The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..'); // Returns: '/foo/bar/baz/asdf'
path.join('foo', {}, 'bar'); // Throws 'TypeError: Path must be a string. Received {}'