Use DATs as a Website

We learned how to put in new data into the response we just sent the browser. But we want to display an actual website without writing the complete website into our callback.
We can just use a textDAT for that by accessing the .text member. Lets try it out by placing a webdat and putting in the following content:

<html>
	<body>
		<p>This is a TouchDesigner website</p>
	</body>
</html>

This is some more text then before. Why do we need it? This is the Hello-World of HTML. Most browser can display a simple text just finde, but better to start doing it right from the beginning. Also note that we can use more then one line of text.
To feed the content of our textDAT as data to the browser, we just feed it into the data entry of our response:

response['data'] = op('text1').text

When refreshing our browser we should see:

But what if we want to host more then one website? Remember that we get some more information in the request dictionary? Till now we ignored the request completly.
Lets try to add a URI in our browser. Add a debug statement to the callback

debug( request['uri'] )

and open the Textport and DATs from the dialogs menu by pressing Alt + T. In the browser we add /TouchIsSuper to our url.

localhost:9980/TouchIsSuper
When taking a look at the Textport after refreshing we now see a debug statement “/TouchIsSuper”

This URI has the same properties as a path to a component, so we can use it directly in our callbacks to refference a textDAT. As the URI is lead by a slash / we are handling an absolute path. In my case the path to my textDAT is /project1/text1
So lets set this path as our uri

localhost:9980/project1/text1

To get the operator we point to with out URI add

target_operator = op( request['uri'] )

To set the data of our response to the textcontent

response['data'] = target_operator.text

After refreshing the browser probably nothing changes. It is still the same website we used before. So lets copy text1 and put in a different text. To get the content of that textDAT we adjust our url in the browser to

localhost:9980/project1/text2

You might had a typo in your url and got an error at your webServerDAT. If not, just change the URL to something completly different.
This will throw an error, because there is no operator at the wanted destination. So lets implement a way for our webServer to check if there is an operator at the given location. We can to that with a simple if-statement and the is comperator. We check if our target_operator is None. Should this be the case we set the statusCode of our response to 404, so the browser knows that there is nothing at the URI and send it back, stopping out callback before the first line. Otherwise it will just continue as usual.

target_operator = op( request['uri'] )
if target_operator is None:
	response['statusCode'] = 404 #Missing File
	response['statusReason'] = 'Operator does not exist'
	return response

Try playing arround with creating other textDATs and refferencing them.