Skip to content

Commit 9ec0d38

Browse files
committed
Merge pull request markfinger#50 from abdelouahabb/master
Adding another example using Tornado.
2 parents 4b617b8 + 125a7d1 commit 9ec0d38

File tree

12 files changed

+7038
-0
lines changed

12 files changed

+7038
-0
lines changed

examples/Tornado-example/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Running the example
2+
===================
3+
4+
Install the dependencies
5+
6+
```
7+
pip install -r requirements.txt
8+
npm install
9+
```
10+
11+
Start the render server
12+
13+
```
14+
node server.js
15+
```
16+
17+
Start the python server
18+
19+
```
20+
python app.py
21+
```
22+
23+
And visit [http://127.0.0.1:8000](http://127.0.0.1:8000)

examples/Tornado-example/app.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os
2+
import tornado.ioloop
3+
import tornado.httpserver
4+
from tornado.web import RequestHandler
5+
from tornado.gen import coroutine
6+
from react.render import render_component
7+
8+
9+
comments = []
10+
11+
class IndexHandler(RequestHandler):
12+
@coroutine
13+
def get(self):
14+
rendered = render_component(
15+
os.path.join(os.getcwd(), 'static', 'js', 'CommentBox.jsx'),
16+
{
17+
'comments': comments,
18+
'url': '/comments',
19+
'xsrf':self.xsrf_token
20+
},
21+
to_static_markup=False,
22+
)
23+
self.render('index.html', rendered=rendered)
24+
25+
26+
class CommentHandler(RequestHandler):
27+
@coroutine
28+
def post(self):
29+
comments.append({
30+
'author': self.get_argument('author'),
31+
'text': self.get_argument('text'),
32+
})
33+
self.redirect('/')
34+
35+
36+
urls = [
37+
(r"/", IndexHandler),
38+
(r"/comments", CommentHandler),
39+
(r"/(.*)", tornado.web.StaticFileHandler, {"path":r"{0}".format(os.path.join(os.path.dirname(__file__),"static"))}),
40+
]
41+
42+
settings = dict({
43+
"template_path": os.path.join(os.path.dirname(__file__),"templates"),
44+
"static_path": os.path.join(os.path.dirname(__file__),"static"),
45+
"cookie_secret": os.urandom(12),
46+
"xsrf_cookies": True,
47+
"debug": True,
48+
"compress_response": True
49+
})
50+
51+
application = tornado.web.Application(urls,**settings)
52+
53+
54+
if __name__ == "__main__":
55+
server = tornado.httpserver.HTTPServer(application)
56+
server.listen(8000)
57+
tornado.ioloop.IOLoop.instance().start()
58+

examples/Tornado-example/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"babel": "^5.6.14",
5+
"body-parser": "^1.13.2",
6+
"express": "^4.13.1",
7+
"react": "^0.13.3",
8+
"react-render": "^0.3.0"
9+
}
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tornado
2+
react

examples/Tornado-example/server.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var http = require('http');
2+
var express = require('express');
3+
var bodyParser = require('body-parser');
4+
var reactRender = require('react-render');
5+
6+
// Ensure support for JSX files
7+
require('babel/register');
8+
9+
var ADDRESS = '127.0.0.1';
10+
var PORT = 9009;
11+
12+
var app = express();
13+
var server = http.Server(app);
14+
15+
app.use(bodyParser.json());
16+
17+
app.get('/', function(req, res) {
18+
res.end('react render server');
19+
});
20+
21+
app.post('/render', function(req, res) {
22+
reactRender(req.body, function(err, markup) {
23+
var error = null;
24+
if (err) {
25+
error = {
26+
type: err.constructor.name,
27+
message: err.message,
28+
stack: err.stack
29+
};
30+
}
31+
res.json({
32+
error: error,
33+
markup: markup
34+
});
35+
});
36+
});
37+
38+
server.listen(PORT, ADDRESS, function() {
39+
console.log('react render server listening at http://' + ADDRESS + ':' + PORT);
40+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
h2 {
2+
border-bottom: 1px solid #eee;
3+
}
4+
5+
form label {
6+
display: block;
7+
}
8+
9+
form button {
10+
margin-left: 5px;
11+
}

0 commit comments

Comments
 (0)