Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
Routing is the process of determining what should happen when a URL is called, or also which parts of the application should handle a specific incoming request.
In the Hello World example we had:
app.get('/', (c) => { /* */ })
That maps GET / to the handler. You can also use app.post(), app.put(), and so on.
Named parameters
To use a value in the URL path (not the query string), use named parameters—for example, a route that takes a string and returns it uppercase:
app.get('/:test', c => {
console.log(c.req.param('test'))
})
Example:
app.get('/uppercase/:val', (c) => {
return c.text(c.req.param('val').toUpperCase())
})
If we send a request to /uppercase/test, we’ll get TEST in the body of the response.
You can use multiple named parameters in one URL; c.req.param() then returns an object with all of them.
Lessons in this unit:
| 0: | Introduction |
| 1: | Your first Hono app |
| 2: | The Request object |
| 3: | Send a response to the client |
| 4: | Manage cookies |
| 5: | Work with HTTP headers |
| 6: | Handling redirects |
| 7: | ▶︎ Routing |
| 8: | JSX templates |
| 9: | Middleware |
| 10: | Hono on Node.js |