Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.
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
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# React comment box example
# React Tutorial

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

## To use

```
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:

### Node

```sh
npm install
node server.js
```

And visit http://localhost:3000/. Try opening multiple tabs!
### Python

```sh
python server.py
```

### Ruby
```sh
ruby server.rb
```

And visit <http://localhost:3000/>. Try opening multiple tabs!
6 changes: 6 additions & 0 deletions _comments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"author": "Pete Hunt",
"text": "Hey there!"
}
]
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"example"
],
"author": "petehunt",
"license": "MIT",
"bugs": {
"url": "https://github.com/reactjs/react-tutorial/issues"
},
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions index.html → public/index.html
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<title>Hello React</title>
<!-- Not present in the tutorial. Just for basic styling. -->
<link rel="stylesheet" href="css/base.css" />
<script src="http://fb.me/react-0.10.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.10.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.11.1/react.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.11.1/JSXTransformer.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
</head>
<body>
Expand Down
14 changes: 13 additions & 1 deletion scripts/example.js → public/scripts/example.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
/** @jsx React.DOM */
/**
* This file provided by Facebook is for non-commercial testing and evaluation purposes only.
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @jsx React.DOM
*/

var converter = new Showdown.converter();

Expand Down
18 changes: 16 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/**
* This file provided by Facebook is for non-commercial testing and evaluation purposes only.
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var fs = require('fs');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

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

app.use('/', express.static(__dirname));
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

Expand Down
57 changes: 57 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
# Facebook reserves all rights not expressly granted.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os
import json
import cgi
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

PUBLIC_PATH = "public"

comments = json.loads(open('_comments.json').read())

def sendJSON(res):
res.send_response(200)
res.send_header('Content-type', 'application/json')
res.end_headers()
res.wfile.write(json.dumps(comments))

class MyHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
root = os.getcwd()
path = PUBLIC_PATH + path
return os.path.join(root, path)

def do_GET(self):
if (self.path == "/comments.json"):
sendJSON(self)
else:
SimpleHTTPRequestHandler.do_GET(self)

def do_POST(self):
if (self.path == "/comments.json"):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type']}
)

# Save the data
comments.append({u"author": form.getfirst("author"), u"text": form.getfirst("text")})
sendJSON(self)
else:
SimpleHTTPRequestHandler.do_POST(self)

if __name__ == '__main__':
print "Server started: http://localhost:3000/"
httpd = HTTPServer(('127.0.0.1', 3000), MyHandler)
httpd.serve_forever()
34 changes: 34 additions & 0 deletions server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
# Facebook reserves all rights not expressly granted.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'webrick'
require 'json'

comments = react_version = JSON.parse(File.read('./_comments.json'))

puts 'Server started: http://localhost:3000/'

root = File.expand_path './public'
server = WEBrick::HTTPServer.new :Port => 3000, :DocumentRoot => root

server.mount_proc '/comments.json' do |req, res|
if req.request_method == 'POST'
# Assume it's well formed
comments << req.query
end

# always return json
res['Content-Type'] = 'application/json'
res.body = comments.to_json
end

trap 'INT' do server.shutdown end

server.start