Skip to content

Commit

Permalink
Merge pull request reactjs#79 from zpao/api
Browse files Browse the repository at this point in the history
Use an API endpoint
  • Loading branch information
zpao committed Sep 26, 2015
2 parents 0ef9282 + aa677d0 commit 4491992
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion public/scripts/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ var CommentForm = React.createClass({
});

React.render(
<CommentBox url="comments.json" pollInterval={2000} />,
<CommentBox url="/api/comments" pollInterval={2000} />,
document.getElementById('content')
);
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- Web page: http://algernon.roboticoverlords.org/
--

handle("/comments.json", function()
handle("/api/comments", function()

-- Set the headers
content("application/javascript")
Expand Down
2 changes: 1 addition & 1 deletion server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 4491992

Please sign in to comment.