Module 11 - Express.js

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following route: app.get("/animals/:id", (req, res) => { }) If the user navigates to "/animals/123?id=24&params=50" what will req.params.id be?

123 This is because param properties are determined by the value following : in the route.

What is a example port that is less restricted?

3001 - this is because it's common practice and fairly easy to remember.

What is the difference between an endpoint and a route?

A route is a URI path used to access the available endpoints. An endpoint performs one or more actions, can accept parameters, and sends back some response.

What happens after you make a request to receive data from an endpoint?

After a short period of time, the server would return the data to the client in the form of a response.

What is the purpose of app?

App represents a single instance of the Express.js server.

Which of the following definitions most accurately describes body parsing middleware.

Body parsing middleware, in the form of `app.use(express.json())`, is a function that parses incoming request bodies before reaching route handlers. That data is then made available in the req.body property.

Which of the following situations is a NOT a use case for the app, Insomnia. Creating boilerplate routes for node.js applications. Saving and organizing frequently used requests into different folders Testing API endpoints before the front end interface is complete. Making requests to API endpoints using JSON, URL encoded form data and more.

Creating boilerplate routes for node.js applications.

What is Express.js and what is it used for?

Express.js is a back-end application framework for Node.js. It provides a flexible way for developers to create web applications and APIs.

True or False? You can use PORT 3000 with an application on Heroku

False Heroku sets the environment variable PORT to 80.

True or False? When serving a front end using Express, we can use still use the VSCode "Open in Browser" tool to open an HTML file in browser.

False - if we don't use the server path, any HTTP calls to the server won't work because the base URL won't be the server's URL.

What are the steps it takes to set up a server?

Instantiate the server and tell it to listen for requests

What's NOT a reason that we use jest.mock() It helps us test pieces of code in isolation It prevents side effects from happening like writing to files or databases. It eliminates the need to create example objects for testing.

It eliminates the need to create example objects for testing - mocking does not eliminate the need for example objects.

What does a machine or program need to be considered a server?

It needs to provide some functionality for the client

How does fs.WriteFileSync() differ from fs.WriteFile()

It's the synchronous version and doesn't require a callback function.

What is the "lib" directory short for and what is it's purpose?

Library and it's purpose is to store files that will be used by multiple files in an application.

What do you need to do before using Insomnia?

Make sure you turn your server on using npm start before testing

How do we require Express.js?

Place this at the top of the initializing file: const express = require("express")

What does the term REST stand for?

Representation State Transfer

What is the purpose of a GET request?

Retrieving data from a server and sending it back to the client.

What do you need to do after running a server and you've made changes to the files?

Stop the previous server (if you haven't stopped it already) by entering Ctrl + C and then Y at the prompt (if prompted) - then run npm start to start the server again.

What are the two major differences between JSON and Javascript object literal notation?

Strings must use double quotes - not single quotes and names must be strings.

Why do we use the name public to hold our front end code?

That was the standard naming convention for it when server were primarily made with Apache web server software. This also can be called "client".

When creating a custom middleware function, what arguments should it accept?

The custom middleware function should accept three arguments: `req`, `res`, and `next`.

What would be in the req.body content of a POST request if you forgot to set up the proper middleware?

The req.body content would be empty.

Where does the / route point us to?

The root of the server - this is the route used to create the homepage for a server.

What is a host?

The website's address

What is a middleware function?

They allow us to keep our route endpoint callback functions more readable while letting us reuse functionality across routes to keep our code DRY (don't repeat yourself).

Why are ports with numbers like 1024 and under considered special by the operating system?

They often require special permissions (like running the process as a administrator.

How do POST requests differ from GET requests?

They represent the action of a client request the server to accept data rather than vice versa.

What's a port?

Think of it as a building number or classroom - it gives the exact destination of the host

What is the * stand for in: app.get("*", (req, res) => { res.sendFile(path.join(__dirname, "./public/index.html)); });

This acts as a wildcard - meaning that any route that wasn't previously defined will fall under this request and recieve the homepage as the response. The order of your routes matter! The wildcard route should always come last - otherwise it will take precedence over named routes.

What does the following code do: app.get("/api/animals", (req, res) => { res.send("Hello!"); });

This will send the string "Hello!" to our client

What would happen if we put "app.listen()" towards the top of our file, right after const app = express()

This would be okay, because app.listen can be placed at any point after app is declared.

True or False? By default fetch() calls are GET requests

True

How many arguments does the get() method require?

Two - one is a string that describes the route the client will have to fetch from and the second is a callback function that will execute every time that route is accessed with a GET request.

How do you create a new Heroku account?

Use the heroku create command in the terminal You can specify the name by adding <app-name> after heroku-create, but it must be a unique name that is not used anywhere else on Heroku.

If we wanted to send a POST request, how can we access that data in the endpoint's callback function?

We use req.body to access any incoming POST data

If we want to send JSON - how would we adjust the following code: app.get("/api/animals", (req, res) => { res.send("Hello!"); });

We would change res.send to res.json We do this to change the headers (i.e. additional metadata that's sent with every request/response) so that the client knows it's receiving JSON.

What happens if we mock "fs"? jest.mock("fs")

We'll be able to execute methods like fs.writeFileSync() without actually writing anything to a file. This will prevent us from accidentally creating new objects when we run a test.

When serving a front end using Express - do the front-end files have to be in the public directory, or can that directory have a different name?

You can name that directory whatever you want - as long as the express.static() middleware uses that directory's name instead of public.

Why can you use the same endpoint (i.e. /api/animals) for both GET and POST requests?

You can use the same endpoint because they are different types of requests. POST method requests to get routed to the endpoint's POST callback function and GET method requests get routed to the endpoint's GET function callback function.

What happens every time you call express()?

You're creating a new Express.js object - changing the properties or methods on one Express.js object doesn't affect other ones you create.

What is the correct syntax for creating a wildcard route?

app.get('/*',function(req,res) { req.send("I am Foo"); });

What's an example of a method to make our server listen?

app.listen(3001, () => { console.log("API server now on port 3001") });

What is some Express.js middleware that instructs the server to make certain files readily available and not gate it behind a server endpoint?

app.use(express.static("public")); This provides a file path to a location in our application (in this case, the public folder) and instructs the server to make these files static resources. That means all of our front-end code can now be accessed without having to create a specific endpoint for it.

What is the correct way to allow serving of static assets from a folder called "public" in the root of your project?

app.use(express.static('public'));

How do we instantiate the server?

const app = express()

When you need to retrieve data from an API, you make a request to an

endpoint

Which of the following methods of the built in Node.js `fs` module? is used in order to write data to the file system?

fs.writeFile()

What command can be used to search for the term "express" inside a set of commit messages?

git log --grep="express"

What command do we use to push to heroku?

git push heroku [FEATURE BRANCH NAME HERE]:main

What command can be used to watch the log files after deploying an application using the Heroku CLI?

heroku logs --tail

Which of the following code snippets is an example of query strings in a URL? http://localhost:3001/people?name=glados&age=999 http://localhost:3001/people/glados http://localhost:3001/people/:name const name = req.query.name

http://localhost:3001/people?name=glados&age=999

What have we learned that allows us to fake things in Jest?

jest.mock()

In the server.js file, we have the following code: (app.use( "/demo-prefix", routes) routes refers to a file that contains this route: router.get("/users", () => ...) What path will you need to get to users?

localhost/demo-prefix/users

What is a shortcut for npm install?

npm i

What command can you use to skip the package questionnaire and leave default answers?

npm init -y

What is the command to install jest?

npm install jest --save-dev

What is the appropriate use case for req.param versus req.query?

req.query is multifaceted, often combining multiple parameters, whereas req.param is specific to a single property, often intended to retrieve a single record.

What is the req parameter short for?

request

What is res parameter short for?

response

What browser API did we use to make HTTP requests from our front-end applications?

the Fetch API's fetch() functionality to make GET requests


Conjuntos de estudio relacionados

Chapter 4,5,6 (Test) missed questions

View Set

Bio 1: Module 1 Launchpad HW Questions

View Set

natuurwetenschappen: de voortplanting bij de mens

View Set