Monday, July 27, 2015

Javascript Backend: NodeJS Tornado Web


NodeJS allows you easily move code back and forth from the client to server, and use the same libraries client-side and server-side. Python3 offers nothing new: still GIL locked, no JIT, and no new syntax worth even mentioning. The only thing Python3 is good for is wasting CPU cycles, and increasing the rate of climate change.

Its nice to have the option to move your server code to NodeJS without having to manually rewrite it. Rusthon now includes a fake Tornado web module, that emulates the basic features of Tornado by wrapping the NodeJS modules: http, url, and ws. The wrapper source code is in nodejs_tornado.py. You load this with from nodejs.tornado import *.

example

example source code
#backend:javascript
from runtime import *
from nodejs import *
from nodejs.tornado import *

PORT = 8000

class MainHandler( tornado.web.RequestHandler ):

 def get(self, path=None):
  print('path', path)
  if path == 'favicon.ico' or path.endswith('.map'):
   self.write('')
  else:
   self.write( open('index.html').read() )


Handlers = [
 ('/', MainHandler),
]

def main():
 print('')
 app = new(tornado.web.Application( Handlers ))
 app.listen( PORT )
 tornado.ioloop.IOLoop.instance().start()

## start main ##
main()

No comments:

Post a Comment