ES6 Classes & Express Routing

My reading notes for Code Fellows


ES6 Classes & Express Routing

Review: ES6 Classes

Classes are a template for creating ____. Objects.

Can a class declaration be hoisted?

No. Classes must be defined before they can be constructed.

How would you describe a constructor and contextual “this” to a non-technical friend?

Constructors create objects. An object is a set of, typically, noun/adjective pairs that represent the qualities of the object. For instance, I may want to make an object that represents a dog. It might look like this:

{ nose: cold, color: ‘brown’, words: ‘woof’, legs: 4 }

A constructor function is a type of machine that can take in parameters and create an object. If I wanted to make a whole bunch of dogs, I could feed those qualities into the machine and a bunch of different types of dogs would pop out with their unique qualites for nose, color, words, and legs. This saves a lot of time and allows one the ability to create a lot of things really quickly.

The word “this” is used in a constructor function to refer to the object that is being created. In the case of our dog, “this” is referencing that particuklar instance of dog.

Using Express Routing

Within Express, what does routing refer to?

Routing refers to how an application’s endpoints respond to client requests.

What is the difference between a route path and a route method? A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.

Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions.

When is it appropriate to add next as a parameter to a route handler and what must you do if next has been passed to your middleware as a parameter? One should use ‘next’ to bypass the ramining route callbacks. If next has been passed as middleware as a paramter. Next will need to be called to allow the function to continue on to its next thing to process.

Express Routing

What is an Express Router? It is a built in functionality within Express that provides routing APIs like .use, .get, .param, and route.

By what mean do we initialize express.Router() in an express server? One may declare a variable to refer to express.Router() and access its routing APIs as methods. i.e,

const router = express.Router();

router.get(‘/’, function(req, res) { …

What do we use route middleware for? Route middleware in Express is a way to do something before a request is processed. This could be things like checking if a user is authenticated, logging data for analytics, or anything else we’d like to do before we actually spit out information to our user.