Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Restful endpoints #48

Closed
wants to merge 3 commits into from
Closed
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
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 `comments` to fetch or add data. Start a server with one of the following:

### Node

Expand Down
2 changes: 1 addition & 1 deletion public/scripts/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ var CommentForm = React.createClass({
});

React.render(
<CommentBox url="comments.json" pollInterval={2000} />,
<CommentBox url="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 @@ -89,7 +89,7 @@ func main() {
if port == "" {
port = "3000"
}
http.HandleFunc("/comments.json", handleComments)
http.HandleFunc("/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('/comments', function(req, res) {
fs.readFile('comments.json', function(err, data) {
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
});

app.post('/comments.json', function(req, res) {
app.post('/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.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function routeRequest()
case '/':
echo file_get_contents('./public/index.html');
break;
case '/comments.json':
case '/comments':
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('/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 '/comments' do |req, res|
comments = JSON.parse(File.read('./comments.json'))

if req.request_method == 'POST'
Expand Down