-
Notifications
You must be signed in to change notification settings - Fork 1
Web Server API
Peter Corke edited this page Sep 4, 2018
·
3 revisions
This is still evolving. This example is in examples/webserver
user.m
function user() %#codegen
webserver(8080, 'myserver'); % start webserver on port 8080
sleep(60); % hang around for a minute, then shutdownfunction myserver() % called on every page request
switch (webserver.url())
case '/'
webserver.html('home here');
case '/bob'
stllog('bob');
webserver.html('<html><body>hello <b>from</b> /bob</body></html>');
case '/alice'
vals.a = 1;
vals.b = 2;
webserver.template('templates/alice.html', vals);
case '/duck':
webserver.file('duck.jpg', 'image/jpg');The switch statement handles the particular URLs that are requested on the webserver:
-
/just sends simple text to the web browser -
/bobsends an HTML formatted string to the web browser -
/alicesends the contents of the template filetemplates/alice.htmland does the substitutions defined by the structvals. The template variableais replaced with1and so on. The numeric values are converted to strings usingnum2strwhich will convert a row-vector to a space separated sequence of numbers. -
/duck[not implemented yet] will return the contents of the data file with the MIME typeimage/jpgwhich will be rendered in the web browser.
If this is running on the same machine as the web browser, we can access it at localhost. The page alice has the URL localhost:8080/alice. The template file is
<html>
<body>
<p>This is a test page</p>
<p>a = <TMPL_VAR name="a"></p>
<p>b = <TMPL_VAR name="b"></p>
</body>
</html>and the resulting web page shows how the the template substitution, the TMPL_VAR tags, have worked.
