Routing

routing in web apps is essentially a set of rules to decide...

the "code we run" is also called an endpoint or a route or a script or a handler or...

Routing is simple...

Many web app server frameworks have complicated systems for routing, but that complexity is not essential.

Routing can be a simple series of if..else statements, or a switch statement

and most of the fancy framework code is simply to build up a list of matching rules which the server then walks through in first-to-last order.

...but don't reinvent the wheel

Frameworks like Express give you more than the implementation of features like routing and parameter passing

they also give you an interface that will make your calling code easier to read

as well as a shared context of documentation and tutorials so other coders don't have as much to learn before understanding your code

Express Routing

Express Routing Example

In the Hello, Express lesson we saw the following route:

app.get('/', (request, response) => response.send('Hello World!'))

This means,

code explanation
app my application,
.get when the client does a GET request
(request, response) => will call this handler function with a request object and a response object
response.send send a response
('Hello, World') with this string as its body

Express Route Matching Rules

Parameters in Express

Express provides several different "parameters" objects:

Path Parameters in Express

The special character : means "this is a path parameter"

Example:

Path: /hello/Gandalf
Route: /hello/:name
Params: {name: 'Gandalf'}

Express will grab the value from the path itself, and put it into the request.params object for you to use later.

LAB: Hello, Path Friend!

Change your "Hello, Express" server to have the following route:

app.get('/hello/:friend', (request, response)=> {
    response.send('Hello, ' + request.params.friend + '!')
});

Prove that it works by visiting http://localhost:5000/hello/Gandalf (or use your own name)

LAB: Hello, You!

Now add a new route

app.get('/hello/:you/from/:me', (request, response)=> {
    response.send(`${request.params.me} says, "Hello, ${request.params.you}!')
});

Does http://localhost:5000/hello/Gandalf/from/Sauron work? If not, why not?

(Answer on next slide.)

Route Matching is Top-Down

Remember, Express routes are a list of matching rules which the server then walks through in first-to-last order.

So if an early route matches, it wins... even if there's a more specific rule that also matches later in the list.

Solution:

Put more specific rules above more general rules.

app.get('/hello/:you/from/:me', (request, response)=> { ... 
});

app.get('/hello/:friend', (request, response)=> { ... 
});

Query Parameters in Express

For query parameters like ?season=winter&weather=cold

Express will grab the name and value from the query string, and put it into the request.query object for you to use later

LAB: Hello, Query Friend!

Now change your "Hello, Express" server to have the following route:

app.get('/hello', (request, response)=> {
    response.send('Hello, ' + request.query.friend + '!')
});

Prove that it works by visiting http://localhost:5000/hello?friend=Gandalf (or use your own name)

Body Parameters in Express

Since request bodies can appear in several different formats, you need to use the correct middleware to extract them.

Example (from the express guide):

// POST /login gets urlencoded bodies
app.post('/login', express.urlencoded(), function (req, res) {
  res.send('welcome, ' + req.body.username)
})

// POST /api/users gets JSON bodies
app.post('/api/users', express.json(), function (req, res) {
  // create user in req.body
})

 Previous Next 

Outline

[menu]

/