CoffeeScript

mschwartz edited this page Feb 11, 2012 · 4 revisions

CoffeeScript Support

CoffeeScript Pages

As a traditional HTTP type server, SilkJS serves static content from the directory specified in the Config.documentRoot variable, with a traditional URI scheme. If the file to be served ends with .coffee, the file is loaded and compiled (the first time), cached, then run. The full SilkJS API is available as well as any JavaScript (or CoffeeScript - see below) extensions you include when starting the server.

Each successive request for the .coffee file cause the server to check the modification time of the file on disk. If the file is newer, it is loaded, compiled, and cached as you expect. If the file is not newer, the cached version is run.

The effect of all this is that SilkJS HTTPD server implements the CoffeeScript "watch" behavior. It's automatic - just like if you edit a .php file on a PHP server you see the change next time you load the page.

Additionally, SilkJS HTTPD looks for index.html, index.jst, index.md, and index.coffee in a directory if the URI ends with a /.

Use case

docroot/ctest.coffee:

res.write '<pre>'
res.write 'hello, world\n'
res.write Util.print_r req

Point your browser at http://localhost:9090/ctest.coffee

Output in browser:

hello, world
(object) :
 [init] : (function)
 [getHeader] : (function)
 [close] : (function)
 [start] : (number) 1328970431640
 [headers] : (object) :
   [host] : (string) localhost:9090
   [connection] : (string) keep-alive
   [cache-control] : (string) max-age=0
   [user-agent] : (string) Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
   [accept] : (string) text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
   [accept-encoding] : (string) gzip,deflate,sdch
   [accept-language] : (string) en-US,en;q=0.8
   [accept-charset] : (string) ISO-8859-1,utf-8;q=0.7,*;q=0.3
   [cookie] : (string) othello_login=c631bf96511b78a85db73b8682731e44
   [] : (string) (empty)
   [apply] : (function)
   [dump] : (function)
 [host] : (string) localhost
 [port] : (string) 9090
 [method] : (string) GET
 [uri] : (string) /ctest.coffee
 [proto] : (string) HTTP/1.1
 [remote_addr] : (string) 127.0.0.1
 [data] : (object) :
   [apply] : (function)
   [dump] : (function)
 [path] : (string) /home/mschwartz/src/SilkJS/httpd/docroot/ctest.coffee
 [apply] : (function)
 [dump] : (function)

Extending the HTTP Server with CoffeeScript

SilkJS now fully supports CoffeeScript as an alternative to or companion to JavaScript for server-side applications.

The include() (see lib/include.js) and require() (see lib/require.js) functions now look for files ending with .js or .coffee. If .coffee extension is present, the file is loaded and compiled into JavaScript, then executed in the global context.

Use Cases

main.js:

include('lib/include.js');
include('lib/require.js');

coffee/main.coffee:

include 'MyClass.coffee'
include 'main_action.coffee'

coffee/myClass.coffee:

class Animal
  constructor: (@name) ->

  move: (meters) ->
    res.write @name + " moved #{meters}m.\n"

class Snake extends Animal
  move: ->
    res.write "Slithering...\n"
    super 5

class Horse extends Animal
  move: ->
    res.write "Galloping...\n"
    super 45

coffee/main_action.coffee

global.main_action = () ->
    res.write '<pre>'
    sam = new Snake "Sammy the Python"
    tom = new Horse "Tommy the Palomino"

    sam.move()
    tom.move()
    res.stop()

From the SilkJS top directory:

./silkjs httpd/main.js coffeescript/main.coffee

Point your browser at http://localhost:9090/

Sammy the Python moved 5m.
Galloping...
Tommy the Palomino moved 45m.

It's FAST!

$ ab -t 30 -c 100 -k http://localhost:9090/

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:        SILK
Server Hostname:        localhost
Server Port:            9090

Document Path:          /
Document Length:        89 bytes

Concurrency Level:      100
Time taken for tests:   1.154 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    50000
Total transferred:      13650000 bytes
HTML transferred:       4450000 bytes
Requests per second:    43329.06 [#/sec] (mean)
Time per request:       2.308 [ms] (mean)
Time per request:       0.023 [ms] (mean, across all concurrent requests)
Transfer rate:          11551.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       3
Processing:     0    1   1.2      1      44
Waiting:        0    1   1.2      1      44
Total:          0    1   1.3      1      44

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      3
  98%      5
  99%      6
 100%     44 (longest request)