10. A web application
In the last step, the task list becomes a web page. Save this program as app.nvs:
<?nvs// This program is a small web application. `nvs serve` runs it once for each request.class Task{ public string $title;
public function constructor(string $title) { $this->title = $title; }}
class Pages{ public static function list(array<Task> $tasks): Core\Html\Markup { var $items = html``; foreach ($tasks as Task $task) { string $title = $task->title; $items = $items + html`<li>{$title}</li>`; } return html`<h1>Tasks</h1><ul>{$items}</ul>`; }}
array<Task> $tasks = [new Task('Buy milk'), new Task('Call Ada')];
// `Core\Request::path()` returns the path of the request, such as `/`.if (Core\Request::path() == '/') { echo Pages::list($tasks), "\n";} else { // Any other path gets the status 404. Core\Response::setStatus(404); echo html`<h1>Not found</h1>`, "\n";}<h1>Tasks</h1><ul><li>Buy milk</li><li>Call Ada</li></ul>Start the server:
nvs serve app.nvsOpen http://127.0.0.1:8000/ in your browser. The page shows the two tasks. Any other path, such
as /tasks, shows “Not found” with the status 404.
nvs serve runs the program once for each request. Core\Request::path() returns the path of the
request, for example /tasks. Each request starts with its own memory, so one request never sees the data of
another.
The output above is the HTML for the path /.
You have finished the Tour. Next, read Syntax to learn the language in order, or look up a class in Reference.
