Skip to content

Commit c631bff

Browse files
committed
added functionality to all 3 servers to write to '_comments.json' within the post request
1 parent 3d89511 commit c631bff

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

_comments.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
[
2-
{
3-
"author": "Pete Hunt",
4-
"text": "Hey there!"
5-
}
6-
]
1+
[{"author":"Pete Hunt","text":"Hey there!"}]

server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ app.get('/comments.json', function(req, res) {
2929

3030
app.post('/comments.json', function(req, res) {
3131
comments.push(req.body);
32+
fs.writeFile('_comments.json', JSON.stringify(comments))
3233
res.setHeader('Content-Type', 'application/json');
3334
res.send(JSON.stringify(comments));
3435
});

server.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
PUBLIC_PATH = "public"
1818

19-
comments = json.loads(open('_comments.json').read())
19+
file = open('_comments.json', 'r+')
20+
comments = json.loads(file.read())
21+
file.close()
2022

2123
def sendJSON(res):
2224
res.send_response(200)
@@ -47,6 +49,12 @@ def do_POST(self):
4749

4850
# Save the data
4951
comments.append({u"author": form.getfirst("author"), u"text": form.getfirst("text")})
52+
print comments
53+
# Write to file
54+
file = open('_comments.json', 'w+')
55+
file.write(json.dumps(comments))
56+
file.close()
57+
5058
sendJSON(self)
5159
else:
5260
SimpleHTTPRequestHandler.do_POST(self)

server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
if req.request_method == 'POST'
2323
# Assume it's well formed
2424
comments << req.query
25+
File.write('./_comments.json', comments.to_json)
2526
end
2627

2728
# always return json

0 commit comments

Comments
 (0)