Skip to content

Commit 558e0c7

Browse files
committed
Merge pull request #7 from darthneel/master
Added write functionality to all three servers
2 parents 8086bbb + 7105db6 commit 558e0c7

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

server.js

+1
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

+8-1
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,11 @@ def do_POST(self):
4749

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

server.rb

+1
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)