From aa729bb214f55396069e7f6246da34b30a112c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 17:49:50 -0700 Subject: [PATCH 1/8] move initial comments to json file --- _comments.json | 6 ++++++ server.js | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 _comments.json diff --git a/_comments.json b/_comments.json new file mode 100644 index 00000000..ea8ae2ce --- /dev/null +++ b/_comments.json @@ -0,0 +1,6 @@ +[ + { + "author": "Pete Hunt", + "text": "Hey there!" + } +] diff --git a/server.js b/server.js index 7724fcf1..3f07797a 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,9 @@ +var fs = require('fs'); 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(bodyParser.json()); From 0995bf029779aeb7013f3bde3331a97d3315af08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 17:52:28 -0700 Subject: [PATCH 2/8] serve static files from public folder --- {css => public/css}/base.css | 0 index.html => public/index.html | 0 {scripts => public/scripts}/example.js | 0 server.js | 3 ++- 4 files changed, 2 insertions(+), 1 deletion(-) rename {css => public/css}/base.css (100%) rename index.html => public/index.html (100%) rename {scripts => public/scripts}/example.js (100%) diff --git a/css/base.css b/public/css/base.css similarity index 100% rename from css/base.css rename to public/css/base.css diff --git a/index.html b/public/index.html similarity index 100% rename from index.html rename to public/index.html diff --git a/scripts/example.js b/public/scripts/example.js similarity index 100% rename from scripts/example.js rename to public/scripts/example.js diff --git a/server.js b/server.js index 3f07797a..f93ccf50 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,12 @@ var fs = require('fs'); +var path = require('path'); var express = require('express'); var bodyParser = require('body-parser'); var app = express(); 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})); From 9e0ecad60198a26d1fdf0a200492b2c2c5c31b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 17:54:08 -0700 Subject: [PATCH 3/8] -x --- public/css/base.css | 0 public/index.html | 0 public/scripts/example.js | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 public/css/base.css mode change 100755 => 100644 public/index.html mode change 100755 => 100644 public/scripts/example.js diff --git a/public/css/base.css b/public/css/base.css old mode 100755 new mode 100644 diff --git a/public/index.html b/public/index.html old mode 100755 new mode 100644 diff --git a/public/scripts/example.js b/public/scripts/example.js old mode 100755 new mode 100644 From 5345d4b59dbe3a78c322395a9925df8cd6b21b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 18:07:34 -0700 Subject: [PATCH 4/8] ruby server --- server.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server.rb diff --git a/server.rb b/server.rb new file mode 100644 index 00000000..6bffc738 --- /dev/null +++ b/server.rb @@ -0,0 +1,24 @@ +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 From 2735272f81d604de6f9c8a2798cb74f3e928a5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 19:49:52 -0700 Subject: [PATCH 5/8] python(2) server --- server.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 00000000..fc336be9 --- /dev/null +++ b/server.py @@ -0,0 +1,47 @@ +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() From a634c296c5bcd13c86b2c64b16d31f2d15e0f27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 20:02:47 -0700 Subject: [PATCH 6/8] better readme --- README.md | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cb7dabbc..81d62e73 100644 --- a/README.md +++ b/README.md @@ -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 . Try opening multiple tabs! From df8e057ff0cf5f26a7a3f1126096f524a1c59445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 20:18:31 -0700 Subject: [PATCH 7/8] licenses --- package.json | 1 - public/scripts/example.js | 14 +++++++++++++- server.js | 12 ++++++++++++ server.py | 10 ++++++++++ server.rb | 10 ++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d1ce0454..b92ebf6b 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "example" ], "author": "petehunt", - "license": "MIT", "bugs": { "url": "https://github.com/reactjs/react-tutorial/issues" }, diff --git a/public/scripts/example.js b/public/scripts/example.js index 0d316b6e..dd29cbfd 100644 --- a/public/scripts/example.js +++ b/public/scripts/example.js @@ -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(); diff --git a/server.js b/server.js index f93ccf50..5dafe0ee 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,15 @@ +/** + * 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'); diff --git a/server.py b/server.py index fc336be9..7edb6f65 100644 --- a/server.py +++ b/server.py @@ -1,3 +1,13 @@ +# 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 diff --git a/server.rb b/server.rb index 6bffc738..1838a289 100644 --- a/server.rb +++ b/server.rb @@ -1,3 +1,13 @@ +# 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' From c8b3153b2ca8d4275149eff818977a0a928156f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 22 Aug 2014 20:28:06 -0700 Subject: [PATCH 8/8] upgrade to latest react, jquery, use cdnjs --- public/index.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/index.html b/public/index.html index defed026..937b5212 100644 --- a/public/index.html +++ b/public/index.html @@ -4,9 +4,9 @@ Hello React - - - + + +