¶ Edit

X Delete

2621 views

8 years ago

Handling URLs

This section will show you how to examine the URL and take action based on the path and query string.

You may be used to thinking of the URL as naming a file on the server. With AppJet, there is only one file that always runs, and it is up to you what to do with the "path", the part of the URL after the name of the server.

You use the global variable request.path to access the path, which always starts with a slash ("/").

This app demonstrates a basic way to serve different things based on the path.

var path = request.path;

if (path == "/") {
   printp("Welcome home!");
}
else if (path == "/foo") {
   printp("Foo page!");
}
else {
   printp("Something else: ",path);
}

printp("Navigation: ");
print(UL(LI(link("/","Home")),
        LI(link("/foo","Foo")),
        LI(link("/my/little/pony","Horse of a different color"))));

We also provide a "dispatch" library that handles the most common cases of dealing with the path. When you call dispatch(), it automatically calls functions that you name starting with "get_".

This app serves different pages to different paths using the dispatch library.

print(H1("Fun Stuff"));

function get_main() { // handles the path "/"
    printp("Welcome to my page!");
    printp(link("/page2","Don't click here."));
}

function get_page2() {
    printp("Let's try this again.");
    printp(link("/page3","Do not click here whatever you do."));
}


function get_page3() {
    printp("You never learn, do you?");
    printp(link("/", "Home"));
}

dispatch();
Powered by AppJet on JGate
source
rendered in 0.123s