diff --git a/README.md b/README.md index 80942982..89ea4e77 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is the React comment box example from [the React tutorial](http://facebook. ## 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: +There are several simple server implementations included. They all serve static files from `public/` and handle requests to `/api/comments` to fetch or add data. Start a server with one of the following: ### Node diff --git a/Server.hs b/Server.hs index 577622f8..9a5c747e 100644 --- a/Server.hs +++ b/Server.hs @@ -48,11 +48,11 @@ main = scotty 3000 $ do get "/" $ file "./public/index.html" - get "/comments.json" $ do + get "/api/comments" $ do comments <- liftIO $ readFile "comments.json" json $ fromJust $ (decode comments :: Maybe [Comment]) - post "/comments.json" $ do + post "/api/comments" $ do comments <- liftIO $ BS.readFile "comments.json" let jsonComments = fromJust $ (decode $ fromStrict comments :: Maybe [Comment]) author <- param "author" diff --git a/public/scripts/example.js b/public/scripts/example.js index a77a7c8a..95a19fb7 100644 --- a/public/scripts/example.js +++ b/public/scripts/example.js @@ -121,6 +121,6 @@ var CommentForm = React.createClass({ }); React.render( - , + , document.getElementById('content') ); diff --git a/server.go b/server.go index ef5f4666..9eb5d7d2 100644 --- a/server.go +++ b/server.go @@ -101,7 +101,7 @@ func main() { if port == "" { port = "3000" } - http.HandleFunc("/comments.json", handleComments) + http.HandleFunc("/api/comments", handleComments) http.Handle("/", http.FileServer(http.Dir("./public"))) log.Println("Server started: http://localhost:" + port) log.Fatal(http.ListenAndServe(":"+port, nil)) diff --git a/server.js b/server.js index c439d59d..1379adbb 100644 --- a/server.js +++ b/server.js @@ -22,14 +22,14 @@ app.use('/', express.static(path.join(__dirname, 'public'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); -app.get('/comments.json', function(req, res) { +app.get('/api/comments', function(req, res) { fs.readFile('comments.json', function(err, data) { res.setHeader('Cache-Control', 'no-cache'); res.json(JSON.parse(data)); }); }); -app.post('/comments.json', function(req, res) { +app.post('/api/comments', function(req, res) { fs.readFile('comments.json', function(err, data) { var comments = JSON.parse(data); comments.push(req.body); diff --git a/server.lua b/server.lua index e80bbc59..10df59fc 100644 --- a/server.lua +++ b/server.lua @@ -15,7 +15,7 @@ -- Web page: http://algernon.roboticoverlords.org/ -- -handle("/comments.json", function() +handle("/api/comments", function() -- Set the headers content("application/javascript") diff --git a/server.php b/server.php index 70301a3f..53c19104 100644 --- a/server.php +++ b/server.php @@ -31,7 +31,7 @@ function routeRequest() $uri = $_SERVER['REQUEST_URI']; if ($uri == '/') { echo file_get_contents('./public/index.html'); - } elseif (preg_match('/\/comments.json(\?.*)?/', $uri)) { + } elseif (preg_match('/\/api\/comments(\?.*)?/', $uri)) { if($_SERVER['REQUEST_METHOD'] === 'POST') { $commentsDecoded = json_decode($comments, true); $commentsDecoded[] = ['author' => $_POST['author'], diff --git a/server.py b/server.py index 7dc15c14..7b4920b9 100644 --- a/server.py +++ b/server.py @@ -15,7 +15,7 @@ app = Flask(__name__, static_url_path='', static_folder='public') app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html')) -@app.route('/comments.json', methods=['GET', 'POST']) +@app.route('/api/comments', methods=['GET', 'POST']) def comments_handler(): with open('comments.json', 'r') as file: diff --git a/server.rb b/server.rb index b0e08601..29af13ee 100644 --- a/server.rb +++ b/server.rb @@ -18,7 +18,7 @@ root = File.expand_path './public' server = WEBrick::HTTPServer.new Port: port, DocumentRoot: root -server.mount_proc '/comments.json' do |req, res| +server.mount_proc '/api/comments' do |req, res| comments = JSON.parse(File.read('./comments.json')) if req.request_method == 'POST'