Skip to content

Commit 3d89511

Browse files
committed
Merge pull request reactjs#5 from zpao/better
Add servers in other languages, other minor changes
2 parents 4c1071d + c8b3153 commit 3d89511

File tree

9 files changed

+147
-10
lines changed

9 files changed

+147
-10
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
# React comment box example
1+
# React Tutorial
22

33
This is the React comment box example from [the React tutorial](http://facebook.github.io/react/docs/tutorial.html).
44

55
## To use
66

7-
```
7+
There are several simple server implementations included. They all serve static files from `public/` and handle requests to `comments.json` to fetch or add data. Start a server with one of the following:
8+
9+
### Node
10+
11+
```sh
812
npm install
913
node server.js
1014
```
1115

12-
And visit http://localhost:3000/. Try opening multiple tabs!
16+
### Python
17+
18+
```sh
19+
python server.py
20+
```
21+
22+
### Ruby
23+
```sh
24+
ruby server.rb
25+
```
26+
27+
And visit <http://localhost:3000/>. Try opening multiple tabs!

_comments.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"author": "Pete Hunt",
4+
"text": "Hey there!"
5+
}
6+
]

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"example"
2424
],
2525
"author": "petehunt",
26-
"license": "MIT",
2726
"bugs": {
2827
"url": "https://github.com/reactjs/react-tutorial/issues"
2928
},
File renamed without changes.

index.html renamed to public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<title>Hello React</title>
55
<!-- Not present in the tutorial. Just for basic styling. -->
66
<link rel="stylesheet" href="css/base.css" />
7-
<script src="http://fb.me/react-0.10.0.js"></script>
8-
<script src="http://fb.me/JSXTransformer-0.10.0.js"></script>
9-
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
7+
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.11.1/react.js"></script>
8+
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.11.1/JSXTransformer.js"></script>
9+
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1010
<script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
1111
</head>
1212
<body>

scripts/example.js renamed to public/scripts/example.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
/** @jsx React.DOM */
1+
/**
2+
* This file provided by Facebook is for non-commercial testing and evaluation purposes only.
3+
* Facebook reserves all rights not expressly granted.
4+
*
5+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
9+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*
12+
* @jsx React.DOM
13+
*/
214

315
var converter = new Showdown.converter();
416

server.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
/**
2+
* This file provided by Facebook is for non-commercial testing and evaluation purposes only.
3+
* Facebook reserves all rights not expressly granted.
4+
*
5+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
9+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
var fs = require('fs');
14+
var path = require('path');
115
var express = require('express');
216
var bodyParser = require('body-parser');
317
var app = express();
418

5-
var comments = [{author: 'Pete Hunt', text: 'Hey there!'}];
19+
var comments = JSON.parse(fs.readFileSync('_comments.json'))
620

7-
app.use('/', express.static(__dirname));
21+
app.use('/', express.static(path.join(__dirname, 'public')));
822
app.use(bodyParser.json());
923
app.use(bodyParser.urlencoded({extended: true}));
1024

server.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
2+
# Facebook reserves all rights not expressly granted.
3+
#
4+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
9+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
11+
import os
12+
import json
13+
import cgi
14+
from BaseHTTPServer import HTTPServer
15+
from SimpleHTTPServer import SimpleHTTPRequestHandler
16+
17+
PUBLIC_PATH = "public"
18+
19+
comments = json.loads(open('_comments.json').read())
20+
21+
def sendJSON(res):
22+
res.send_response(200)
23+
res.send_header('Content-type', 'application/json')
24+
res.end_headers()
25+
res.wfile.write(json.dumps(comments))
26+
27+
class MyHandler(SimpleHTTPRequestHandler):
28+
def translate_path(self, path):
29+
root = os.getcwd()
30+
path = PUBLIC_PATH + path
31+
return os.path.join(root, path)
32+
33+
def do_GET(self):
34+
if (self.path == "/comments.json"):
35+
sendJSON(self)
36+
else:
37+
SimpleHTTPRequestHandler.do_GET(self)
38+
39+
def do_POST(self):
40+
if (self.path == "/comments.json"):
41+
form = cgi.FieldStorage(
42+
fp=self.rfile,
43+
headers=self.headers,
44+
environ={'REQUEST_METHOD':'POST',
45+
'CONTENT_TYPE':self.headers['Content-Type']}
46+
)
47+
48+
# Save the data
49+
comments.append({u"author": form.getfirst("author"), u"text": form.getfirst("text")})
50+
sendJSON(self)
51+
else:
52+
SimpleHTTPRequestHandler.do_POST(self)
53+
54+
if __name__ == '__main__':
55+
print "Server started: http://localhost:3000/"
56+
httpd = HTTPServer(('127.0.0.1', 3000), MyHandler)
57+
httpd.serve_forever()

server.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
2+
# Facebook reserves all rights not expressly granted.
3+
#
4+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
9+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
11+
require 'webrick'
12+
require 'json'
13+
14+
comments = react_version = JSON.parse(File.read('./_comments.json'))
15+
16+
puts 'Server started: http://localhost:3000/'
17+
18+
root = File.expand_path './public'
19+
server = WEBrick::HTTPServer.new :Port => 3000, :DocumentRoot => root
20+
21+
server.mount_proc '/comments.json' do |req, res|
22+
if req.request_method == 'POST'
23+
# Assume it's well formed
24+
comments << req.query
25+
end
26+
27+
# always return json
28+
res['Content-Type'] = 'application/json'
29+
res.body = comments.to_json
30+
end
31+
32+
trap 'INT' do server.shutdown end
33+
34+
server.start

0 commit comments

Comments
 (0)