Chapter 3. Appmods: Dynamic Content in Yaws

When developing a web service, there are times when the developer does not want to map the URL that is sent by the web browser to an actual file. This may be because all data is stored in a database or is generated dynamically. In this case you want the browser to go to some URL and have the server create the content and return it.

Appmods are to Yaws what Apache modules are to Apache—a way to create a custom handler for a URL in the web server. When using an appmod, the browser will send a standard URL, but instead of having a static file sent back to the user, Yaws will execute some Erlang code and send the response back to the user. This allows for fully dynamic content in the Yaws web server.

The difference is that in the case of an appmod, out/1 will need to create the full content of the response, while in a .yaws file it may just be a small part of it. In many cases, the response will not be HTML but will be some other format, such as XML (XML), JSON (JSON), or even something like an audio file, CSV text file, or PDF.

In addition, by using an appmod you can break the association between a file on the local disk of a node and the URL representation that is presented. So it is possible to present a URL like /blog-posts/2011-Dec-02/why-you-should-use-erlang.html without there actually being a file at that path (or even a directory structure)—just some code that knows how to map that URL onto some logical resource that can then be constructed. ...

Get Building Web Applications with Erlang now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.