Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/Tornado-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Running the example
===================

Install the dependencies

```
pip install -r requirements.txt
npm install
```

Start the render server

```
node server.js
```

Start the python server

```
python app.py
```

And visit [http://127.0.0.1:8000](http://127.0.0.1:8000)
58 changes: 58 additions & 0 deletions examples/Tornado-example/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import tornado.ioloop
import tornado.httpserver
from tornado.web import RequestHandler
from tornado.gen import coroutine
from react.render import render_component


comments = []

class IndexHandler(RequestHandler):
@coroutine
def get(self):
rendered = render_component(
os.path.join(os.getcwd(), 'static', 'js', 'CommentBox.jsx'),
{
'comments': comments,
'url': '/comments',
'xsrf':self.xsrf_token
},
to_static_markup=False,
)
self.render('index.html', rendered=rendered)


class CommentHandler(RequestHandler):
@coroutine
def post(self):
comments.append({
'author': self.get_argument('author'),
'text': self.get_argument('text'),
})
self.redirect('/')


urls = [
(r"/", IndexHandler),
(r"/comments", CommentHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path":r"{0}".format(os.path.join(os.path.dirname(__file__),"static"))}),
]

settings = dict({
"template_path": os.path.join(os.path.dirname(__file__),"templates"),
"static_path": os.path.join(os.path.dirname(__file__),"static"),
"cookie_secret": os.urandom(12),
"xsrf_cookies": True,
"debug": True,
"compress_response": True
})

application = tornado.web.Application(urls,**settings)


if __name__ == "__main__":
server = tornado.httpserver.HTTPServer(application)
server.listen(8000)
tornado.ioloop.IOLoop.instance().start()

10 changes: 10 additions & 0 deletions examples/Tornado-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"private": true,
"dependencies": {
"babel": "^5.6.14",
"body-parser": "^1.13.2",
"express": "^4.13.1",
"react": "^0.13.3",
"react-render": "^0.3.0"
}
}
2 changes: 2 additions & 0 deletions examples/Tornado-example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tornado
react
40 changes: 40 additions & 0 deletions examples/Tornado-example/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var reactRender = require('react-render');

// Ensure support for JSX files
require('babel/register');

var ADDRESS = '127.0.0.1';
var PORT = 9009;

var app = express();
var server = http.Server(app);

app.use(bodyParser.json());

app.get('/', function(req, res) {
res.end('react render server');
});

app.post('/render', function(req, res) {
reactRender(req.body, function(err, markup) {
var error = null;
if (err) {
error = {
type: err.constructor.name,
message: err.message,
stack: err.stack
};
}
res.json({
error: error,
markup: markup
});
});
});

server.listen(PORT, ADDRESS, function() {
console.log('react render server listening at http://' + ADDRESS + ':' + PORT);
});
11 changes: 11 additions & 0 deletions examples/Tornado-example/static/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
h2 {
border-bottom: 1px solid #eee;
}

form label {
display: block;
}

form button {
margin-left: 5px;
}
Loading