Chapter 7: Wildcards and Catch-All in Go

Match multiple path segments. In this chapter, you will learn wildcards and catch-all in depth with Go code examples, explanations, and best practices.

Overview

This chapter covers wildcards and catch-all for Kungfu.js developers using Go. We will start with the basics, move through practical examples, and end with advanced techniques and common pitfalls.

Why This Matters

Understanding wildcards and catch-all is essential because it is a core part of building web applications. Every real-world app needs to handle match multiple path segments. Skipping this chapter would leave a gap in your knowledge that would cause problems later.

Code Example

This chapter covers wildcards and catch-all in Go. Below is a practical code example you can copy, run, and modify to solidify your understanding.

// Wildcards and Catch-All in Go
// This example demonstrates wildcards and catch-all with a runnable server.
const { Kungfu } = require('@kungfu/core');

const app = new Kungfu();

app.get('/', (req) => {
    return { status: 200, body: 'Kungfu.js is running. Try /hello or /api/health' };
});

app.get('/hello', (req) => {
    return {
        status: 200,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ message: 'world' })
    };
});

app.get('/api/health', (req) => {
    return {
        status: 200,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ status: 'ok', uptime_seconds: 0 })
    };
});

console.log('Server running on http://localhost:3000');
app.listen(3000);

Why This Matters

Every feature in Kungfu.js is designed to be predictable: the same input always produces the same output, the same route always hits the same handler, and the same middleware always runs in the same order. This predictability is what makes production apps maintainable. When something breaks at 3 AM, you need to reason about the request path quickly — and the onion-model middleware plus the trie router give you that mental model for free.

Common Mistakes

  • Not reading the documentation: Always check the API reference when something does not work as expected.
  • Skipping security: Never disable the default middleware unless you have a very good reason. Security is not optional.
  • Not testing: Write tests for your handlers. Kungfu.js makes this easy with the built-in test utilities.

Summary

In this chapter, you learned about wildcards and catch-all in Go. You saw code examples, understood how things work under the hood, and learned about common mistakes to avoid.

What is Next?

In chapter 8, we will cover Query Strings: Parse and use query parameters.