Skip to content

Commit 4491992

Browse files
committed
Merge pull request reactjs#79 from zpao/api
Use an API endpoint
2 parents 0ef9282 + aa677d0 commit 4491992

File tree

9 files changed

+11
-11
lines changed

9 files changed

+11
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is the React comment box example from [the React tutorial](http://facebook.
66

77
## To use
88

9-
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:
9+
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:
1010

1111
### Node
1212

Server.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ main = scotty 3000 $ do
4848

4949
get "/" $ file "./public/index.html"
5050

51-
get "/comments.json" $ do
51+
get "/api/comments" $ do
5252
comments <- liftIO $ readFile "comments.json"
5353
json $ fromJust $ (decode comments :: Maybe [Comment])
5454

55-
post "/comments.json" $ do
55+
post "/api/comments" $ do
5656
comments <- liftIO $ BS.readFile "comments.json"
5757
let jsonComments = fromJust $ (decode $ fromStrict comments :: Maybe [Comment])
5858
author <- param "author"

public/scripts/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ var CommentForm = React.createClass({
121121
});
122122

123123
React.render(
124-
<CommentBox url="comments.json" pollInterval={2000} />,
124+
<CommentBox url="/api/comments" pollInterval={2000} />,
125125
document.getElementById('content')
126126
);

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func main() {
101101
if port == "" {
102102
port = "3000"
103103
}
104-
http.HandleFunc("/comments.json", handleComments)
104+
http.HandleFunc("/api/comments", handleComments)
105105
http.Handle("/", http.FileServer(http.Dir("./public")))
106106
log.Println("Server started: http://localhost:" + port)
107107
log.Fatal(http.ListenAndServe(":"+port, nil))

server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ app.use('/', express.static(path.join(__dirname, 'public')));
2222
app.use(bodyParser.json());
2323
app.use(bodyParser.urlencoded({extended: true}));
2424

25-
app.get('/comments.json', function(req, res) {
25+
app.get('/api/comments', function(req, res) {
2626
fs.readFile('comments.json', function(err, data) {
2727
res.setHeader('Cache-Control', 'no-cache');
2828
res.json(JSON.parse(data));
2929
});
3030
});
3131

32-
app.post('/comments.json', function(req, res) {
32+
app.post('/api/comments', function(req, res) {
3333
fs.readFile('comments.json', function(err, data) {
3434
var comments = JSON.parse(data);
3535
comments.push(req.body);

server.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
-- Web page: http://algernon.roboticoverlords.org/
1616
--
1717

18-
handle("/comments.json", function()
18+
handle("/api/comments", function()
1919

2020
-- Set the headers
2121
content("application/javascript")

server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function routeRequest()
3131
$uri = $_SERVER['REQUEST_URI'];
3232
if ($uri == '/') {
3333
echo file_get_contents('./public/index.html');
34-
} elseif (preg_match('/\/comments.json(\?.*)?/', $uri)) {
34+
} elseif (preg_match('/\/api\/comments(\?.*)?/', $uri)) {
3535
if($_SERVER['REQUEST_METHOD'] === 'POST') {
3636
$commentsDecoded = json_decode($comments, true);
3737
$commentsDecoded[] = ['author' => $_POST['author'],

server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
app = Flask(__name__, static_url_path='', static_folder='public')
1616
app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html'))
1717

18-
@app.route('/comments.json', methods=['GET', 'POST'])
18+
@app.route('/api/comments', methods=['GET', 'POST'])
1919
def comments_handler():
2020

2121
with open('comments.json', 'r') as file:

server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
root = File.expand_path './public'
1919
server = WEBrick::HTTPServer.new Port: port, DocumentRoot: root
2020

21-
server.mount_proc '/comments.json' do |req, res|
21+
server.mount_proc '/api/comments' do |req, res|
2222
comments = JSON.parse(File.read('./comments.json'))
2323

2424
if req.request_method == 'POST'

0 commit comments

Comments
 (0)