Express Module #54 Flashcards
What is Express.js? What is it used for?
Express. js is a Node. js web application server framework, designed for building single-page, multi-page, and hybrid web applications. It is the de facto standard server framework for node. … js framework for quickly creating dynamic end-to-end REST APIs.
Making a webserver was a dozen or so lines of code using vanilla node.js. Using express it’s only 4 lines. What are those lines?
const express = require('express') const app = express()
app. get(‘/’, (req, res) => res.send(‘Hello World!’))
app. listen(3000, () => console.log(‘Server ready’))
There is a method for every HTTP verb: get( ), post( ), put( ), delete( ), patch( ): How are they written? Hint: they all use the same syntax.
app. get(‘/’, (req, res) => { /* / })
app. post(‘/’, (req, res) => { / / })
app. put(‘/’, (req, res) => { / / })
app. delete(‘/’, (req, res) => { / / })
app. patch(‘/’, (req, res) => { / */ })
The HTTP Verbs accept a callback function. How does that look?
(req, res) => res.send(‘Hello World’)
What objects are returned from the (req, res) => res.send(‘Hello World’) callback
Request and response objects
Request is the HTTP request. It gives us all the request information, including the request parameters, the headers, the body of the request, and more.
Response is the HTTP response object that we’ll send to the client.
What we do in this callback is to send the ‘Hello World!’ string to the client, using the Response.send() method.
What are a few things that are held in the request?
These all seemed fairly self explanatory... .app .baseUrl .body .cookies .hostname .ip .method .params .path .protocol .query .secure .signedCookies .xhr
How do you return query string parameters (‘?’, ‘&’) you see these all the time in urls.
By populating the Request.query object
Example, look at the console.log expression.
const express = require('express') const app = express ( )
app.get('/', (req, res) => { console.log(req.query) })
app.listen(8080)
What happens if there aren’t any query parameters?
Then it returns an empty object
What’s the appropriate loop to use on an empty query parameters object or any empty object?
The for in loop. In this situation, it looks like this:
for (const key in req.query) {
console.log(key, req.query[key])
}
** How can you retrieve the POST query string parameters using express? And what are they most commonly used for?
POST query parameters are sent by HTTP clients for example by forms, or when performing a POST request sending data.
How can you access this data?
If the data was sent as JSON, using Content-Type: application/json, you will use the express.json() middleware:
const express = require('express') const app = express ( ) app.use(express.json( )
How would you send an empty response (without a body)
res.end( )
How do you set the HTTP response header?
Use the Response.status():
Which actually looks like this i action?
res.status(404).end()
or
res.status(404).send(‘File not found’)
res.status(404).end()
or
res.status(404).send(‘File not found’)
What method sends a JSON response?
You can send JSON to the client by using Response.json(), a useful method.
It accepts an object or array, and converts it to JSON before sending it:
res.json({ username: ‘Flavio’ })
How are cookies managed? That is to say which method?
Use the Response.cookie() method to manipulate your cookies.
Examples:
res.cookie(‘username’, ‘Flavio’)
domain The cookie domain name
expires Set the cookie expiration date. If missing, or 0, the cookie is a session cookie
httpOnly Set the cookie to be accessible only by the web server
maxAge Set the expiry time relative to the current time, expressed in milliseconds
path The cookie path. Defaults to ‘/’
secure Marks the cookie HTTPS only
signed Set the cookie to be signed
sameSite Value of SameSite
How can a cookie be cleared
res.clearCookie(‘username’)