1
0

TrivialServer.st 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 obj |
  25. block := [:req :res |
  26. {'res.writeHead(200, {''Content-Type'': ''text/html''});'}.
  27. res end: (self process: req)].
  28. (http createServer: block)
  29. listen: #(1337 '127.0.0.1').
  30. console log: 'TrivialServer running at http://127.0.0.1:1337/'
  31. ! !
  32. !TrivialServer class methodsFor: 'initialization'!
  33. initialize
  34. "We require these Node modules."
  35. {'os = require(''os''), http = require(''http'');'}
  36. !
  37. main
  38. self new start
  39. ! !