Browse Source

Added trivial server example with nodejs.

Göran Krampe 13 years ago
parent
commit
a361ce94bd

+ 8 - 0
nodejs/trivialserver/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

+ 17 - 0
nodejs/trivialserver/README

@@ -0,0 +1,17 @@
+Trivial Node.js example in JTalk:
+
+TrivialServer.st - source code.
+trivial          - trivial bash script to run resulting program.
+main.js          - small "doit" to actually run code. Will be added by ntalkc.
+Makefile         - trivial makefile, read it.
+
+TrivialServer.js - produced when you run make. Contains JTalk + Hello.st + main.js
+
+To play, do this:
+
+make run
+
+...or:
+
+make
+./trivial

+ 33 - 0
nodejs/trivialserver/TrivialServer.st

@@ -0,0 +1,33 @@
+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
+! !
+
+!TrivialServer class methodsFor: 'initialization'!
+initialize
+	{'var os = require(''os'');'}
+! !

+ 10 - 0
nodejs/trivialserver/main.js

@@ -0,0 +1,10 @@
+var http = require('http');
+var os = require('os');
+// Instantiate a JTalk object to process requests
+var server = smalltalk.TrivialServer._new();
+http.createServer(function (req, res) {
+  res.writeHead(200, {'Content-Type': 'text/html'});
+  // Let the JTalk object process it
+  res.end(server._process_(req));
+}).listen(1337, "127.0.0.1");
+console.log('TrivialServer running at http://127.0.0.1:1337/');

+ 1 - 0
nodejs/trivialserver/trivial

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