Browse Source

Added trivialserver2 variation trying to do more in JTalk.

Göran Krampe 13 years ago
parent
commit
423d6f337d

+ 1 - 0
nodejs/README

@@ -7,6 +7,7 @@ nodecompile.js  - trivial Node.js javascript code for compiling .st files, used
 
 hello           - Hello world example with a simple Makefile
 trivialserver   - A slightly larger example.
+trivialserver2  - A variation on trivialserver that tries to do more in JTalk.
 
 You will need "node" in your path to try examples.
 

+ 8 - 0
nodejs/trivialserver2/Makefile

@@ -0,0 +1,8 @@
+Program.js: TrivialServer.st main.js
+	../ntalkc TrivialServer.st Program
+
+run: Program.js
+	./trivial
+
+clean:
+	rm Program.js TrivialServer.js

+ 4 - 0
nodejs/trivialserver2/README

@@ -0,0 +1,4 @@
+Slight variation on ../trivialserver but this one tries to do more Node
+stuff from Smalltalk. Note that we can use a block as a function callback.
+It would be nice if we could improve more on JTalk's ability to call javascript
+code directly.

+ 46 - 0
nodejs/trivialserver2/TrivialServer.st

@@ -0,0 +1,46 @@
+Object subclass: #TrivialServer
+        instanceVariableNames: 'counter'
+        category: 'TrivialServer'!
+
+!TrivialServer methodsFor: 'initializing'!
+initialize
+        counter := 0
+! !
+
+
+!TrivialServer methodsFor: 'processing'!
+process: aRequest
+        | hostname httpVersion stream |
+	counter := counter + 1.
+
+        "Calling a method in a js module"
+	hostname := {'os.hostname()'}.
+
+        "Accessing a property of js HTTP request object"
+        httpVersion := {'aRequest.httpVersion'}.
+
+        stream := String new writeStream.
+	stream
+		nextPutAll: '<html><p>Request HTTP version: ', httpVersion, '</p>';
+		nextPutAll: '<p>OS hostname: ', hostname, '</p>';
+		nextPutAll: '<p>Number of requests: ', counter asString, '</p></html>'.
+	^stream contents
+!
+
+start
+        | block |
+        block := [:req :res |
+           {'res.writeHead(200, {''Content-Type'': ''text/html''});'}.
+           {'res.end(self._process_(req));'}].
+
+        {'http.createServer(block).listen(1337, ''127.0.0.1'');'}.
+        {'console.log(''TrivialServer running at http://127.0.0.1:1337/'');'}
+! !
+
+
+!TrivialServer class methodsFor: 'initialization'!
+initialize
+        "We require these Node modules."
+
+	{'os = require(''os''), http = require(''http'');'}
+! !

+ 1 - 0
nodejs/trivialserver2/main.js

@@ -0,0 +1 @@
+smalltalk.TrivialServer._new()._start()

+ 1 - 0
nodejs/trivialserver2/trivial

@@ -0,0 +1 @@
+node Program.js $@