Express Flashcards
How do you add express to your package dependencies?
npm install express –save (while you’re in the package directory)
What Express application method starts the server and binds it to a network PORT?
app.listen( )
How do you mount a middleware with an Express application?
app.use( )
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.
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
request and response objects
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
app.listen should always be where in the file?
It should always be at the end of the file
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
What does the express.json( ) middleware do and when would you need it?
It parses incoming requests with JSON payloads and is based on body-parser.
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option.
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 does express.static( ) return?
Return Value: It returns a function
A middleware function to be specific
What is the local __dirname variable in a Node.js module?
__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file. It returns the name the of current working directory. It returns the directory name of the directory containing the source code file.
What does the join( ) method of Node’s path module do?
The path. join( ) method joins the specified path segments into one path. You can specify as many path segments as you like. The specified path segments must be strings, separated by comma.
The path.join( ) method is used to join a number of path-segments using the platform-specific delimiter to form a single path.