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

server.js updates comments variable after get request to "comments.json" #17

Merged
merged 3 commits into from
Feb 6, 2015

Conversation

ASwitlyk
Copy link

In the fetching data from the server section of the tutorial documentation, a method to poll the server is added so that the app polls the server every 2 seconds for changes to the server's _comments.json data (which will then reflected through dynamic updates to the DOM). Section of tutorial below:

screen shot 2015-01-22 at 11 15 11 am

Here, in the tutorial, after adding the server polling it encourages you to change the _comments.json file so that you can see the app dynamically change when new information is obtained from the server.

The server file is setup to just fetch json data from a static file (_comments.json) by reading the file and then storing the contents of the file in a variable 'comments'. Then on a get requests to comments.json, the server sends the stringified comments data as a response. The problem is that the _comments.json file is currently only read one time by the server and never reread, so changes to the _comments.json file never change the comments variable.

This push request updates the server.js file so that the server will re-read the _comments.json file and update the comments variable after every get request to comments.json.

@zpao
Copy link
Member

zpao commented Jan 22, 2015

Great timing, I just asked in #15 to add the re-read functionality to all servers.

This looks like a good start, but 2 things.

  1. You're actually rereading the file after the response has been sent, so the new values won't be sent until the next request.
  2. We need to do this in the POST handler as well, otherwise we may overwrite changes in the file.

…ystem read) before sending stringified comments in response.
@ASwitlyk
Copy link
Author

Hey Paul,

I just moved the comments array update from the readFileSync to the top of the GET request, so that the comments array will be updated first before it is sent out in the response.

I originally had the comments update occur after the response was sent because of potential async problems with reading the file system but I realize we're using the synchronous version of readfile so it'll block before updating the comments variable and sending it out.

Also, I don't know if the POST handler needs a change since it looks like on post it already updates the comments array before writing to the _comments.json file and sending out the response.

@zpao
Copy link
Member

zpao commented Jan 22, 2015

If the file is modified after the last GET but before the POST, we'll overwrite those changes with what we have in memory + the new comment. Instead we can just stop storing the comments in memory and only read from + write to disk.

Also FYI: you could use the async fs methods - since we're using express, the response doesn't actually send until res.send is called. You could do that in a callback if you wanted. But I don't actually think it's an important detail here so it's not really worth it :)

@ASwitlyk
Copy link
Author

Removed the comments variable from memory and now all I/O is done from writing or reading from the disk. Also, changed the fs methods from the sync to async methods and put the res.send(s) in their callbacks.

Thank you for your suggestions!

@zpao
Copy link
Member

zpao commented Feb 2, 2015

Ah, it looks like our bot wasn't set up right for this repo. Can you sign the CLA - https://code.facebook.com/cla - and then this is good to go.

@zpao zpao merged commit 3d2d078 into reactjs:master Feb 6, 2015
zpao added a commit that referenced this pull request Feb 6, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants