TrivialServer.st 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. Object subclass: #TrivialServer
  2. instanceVariableNames: 'counter'
  3. category: 'TrivialServer'!
  4. !TrivialServer methodsFor: 'initializing'!
  5. initialize
  6. counter := 0
  7. ! !
  8. !TrivialServer methodsFor: 'processing'!
  9. process: aRequest
  10. | hostname httpVersion stream |
  11. counter := counter + 1.
  12. "Calling a method in a js module"
  13. hostname := {'os.hostname()'}.
  14. "Accessing a property of js HTTP request object"
  15. httpVersion := {'aRequest.httpVersion'}.
  16. stream := String new writeStream.
  17. stream
  18. nextPutAll: '<html><p>Request HTTP version: ', httpVersion, '</p>';
  19. nextPutAll: '<p>OS hostname: ', hostname, '</p>';
  20. nextPutAll: '<p>Number of requests: ', counter asString, '</p></html>'.
  21. ^stream contents
  22. !
  23. start
  24. | block |
  25. block := [:req :res |
  26. {'res.writeHead(200, {''Content-Type'': ''text/html''});'}.
  27. {'res.end(self._process_(req));'}].
  28. {'http.createServer(block).listen(1337, ''127.0.0.1'');'}.
  29. {'console.log(''TrivialServer running at http://127.0.0.1:1337/'');'}
  30. ! !
  31. !TrivialServer class methodsFor: 'initialization'!
  32. initialize
  33. "We require these Node modules."
  34. {'os = require(''os''), http = require(''http'');'}
  35. !
  36. main
  37. self new start
  38. ! !