Did you know ... | Search Documentation: |
Hello World! |
Below is the source from our first web-server. It serves a single page,
named /hello_world
, which returns a simple plain-text message. Note
the three elements of the page:
root(hello_world)
uses an alias-mechanism similar to
absolute_file_name/3 and allows for moving parts of the server
locations easily. See http_absolute_location/3. We could also have
used '/hello_world'
.Content-type:
<mime-type> header. Printing can be
done using any Prolog printing predicate, but the format-family
is the most useful. See format/2.:- use_module(library(http/thread_httpd)). :- use_module(library(http/http_dispatch)). :- http_handler(root(hello_world), say_hi, []). % (1) server(Port) :- % (2) http_server(http_dispatch, [port(Port)]). say_hi(_Request) :- % (3) format('Content-type: text/plain~n~n'), format('Hello World!~n').
To run this server, put the above code in a file, load the file into Prolog and run the goal below. 5000 is the port-number. This can be any number between 1001 and 65000 (depending on your OS). Now direct your browser to http://localhost:5000/hello_world and enjoy your first Prolog-based web-server!
?- server(5000).
Oops, we do not need that. The full manual gives options for redirecting requests from Apache to our server, so that your server is reachable on the default port 80. Actually, the SWI-Prolog HTTP server libraries are very much like Tomcat, but now for Prolog. The SWI-Prolog website is handled by SWI-Prolog, redirected from an Apache server.