Dynamic Paths

Dynamic Paths

When adding paths, it is possible to have HTGM match path based on a pattern, rather than an exact string. Doing so makes path matching dynamic. An example of a dynamic path is /demos/users/{user_name}, which will match a client accessing that URL by that pattern

Wildcard Paths

The simplest type of dynamic path is a wildcard path, where the path string contains an asterisk *, which will match any value.

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a path
global.server.add_path("demos/wildcard/*/path", function(_context) {
	_context.response.send_html("<h1>Wildcard match</h1>")
});
		
// This must run after entering the first room
global.server.start();

In the above example, the wildcard path demos/wildcard/*/path is added. This will match any request that follows this pattern, where * can be anything. For example, both the following URLs will match this pattern.

However the following do not match. These will return a not-found page.

Wildcard End Paths

Normally asterisk will only match one segment of the path, e.g. demo/wildcard/*/path will not match demo/wildcard/two/segments/path. However the exception is if the wildcard is on the end of the path, which will cause it to match any path regardless of how many segments.

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a path
global.server.add_path("demos/wildcard_end/*", function(_context) {
	_context.response.send_html("<h1>Wildcard end match</h1>")
});
		
// This must run after entering the first room
global.server.start();

In the above example, the wildcard path demos/wildcard_end/* is added. This will match any request that begins with demos/wildcard_end/ and can have any ending, for example:

However the it will not match a blank value. So the following will return a not-found page

Path Parameters

In addition to wildcard paths, the path can also capture the pattern and make the value available as a path parameter, allowing you to create paths that dynamically render content based on what was requested

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a path
global.server.add_path("demos/fruit/{fruit_name}/like", function(_context) {
	_context.response.send_html($"<h1>I like {_context.request.get_parameter("fruit_name")}</h1>")
});
		
// This must run after entering the first room
global.server.start();

In the above example, a path pattern of demos/fruit/{fruit_name}/like is added. This makes the variable fruit_name avalaible in the context to be used during rendering. For example:

Multiple Path Parameters

Multiple path parameters can be used together

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a path
global.server.add_path("demos/users/{user_name}/likes_fruit/{fruit_name}", function(_context) {
	var _user_name = _context.request.get_parameter("user_name");
	var _fruit_name = _context.request.get_parameter("fruit_name");
	_context.response.send_html($"<h1>{_user_name} likes {_fruit_name}</h1>")
});
		
// This must run after entering the first room
global.server.start();

In the above example, a path pattern of demos/users/{user_name}/likes_fruit/{fruit_name} is added. This makes the variables user_name and fruit_name boht avalaible in the context to be used during rendering. For example: