问题描述:

I'm experimenting with dynamic(ish) server less sites, (no database, serversided languages, etc.) which is why I am hoping to only use JavaScript/AJAX/jQuery. I want to be able to serve a dynamic(ish) webpage with static files.

One solution I came up with is to essentially use text files(json, xml) on the same server as the webpage to serve as a datastore.

But I could only find ways to load the entire file rather than specific parts of it. Which isn't really a big problem since the text files are small, however the text files could potentially grow to be large.

If you could load the file line by line, the payload to the client would be faster, and the JavaScript parsing would be sped up significantly.

If I am only interested in fresh data, I was hoping I could prepend the data to the top of the file when populating the file and read it in line by line (since I would only be interested in say 10 lines per page). Also access to the middle of a file would be incredibly useful.

I just want to avoid having to send the entire file to a client(not for security but for space/time efficiency).

Is there any way to do this in JavaScript? Or am I missing an obvious solution?

网友答案:

I haven't done this before, but you should have a chance by having a look at the Range, If-Range and Accept-Ranges HTTP headers.

I did a short test by accessing or webserver at http://www.stadt-salzburg.at/. It sends an Accept-Ranges:bytes response header for the static html file resource of the start page which means that it accepts Range-requests. Using the Firefox extension Modify Headers I then specified a request header Range:bytes=257-2048 and the server really returned only the requested part of this file.

According to the HTTP 1.1 header field specification you should also be able to determine if the file has been modified and do some other nice stuff :-) As I'm not very experienced in HTTP headers I would like to ask you to have a look yourself if you can achieve your desired result.

网友答案:

I worked on this same problem a few years ago. JavaScript on its own won't cut it - someone please prove me wrong.

  1. Add data to your local text file in real-time in the normal way (append new data to the end).
  2. have a cgi app running on the local machine that can perform an OPEN, SEEK, READ, CLOSE of one record at a time.
  3. return the new/next file offset and the text of the record to the app running in javascript.
  4. the javascript remembers the next record offset - or can set it to zero to re-process the entire fle from its start.
  5. the javascript calls the cgi with the next record offset.
  6. the cgi app receives the request and does its OPEN, SEEK, READ, CLOSE. if no new data is available the read will load zero bytes and the cgi app can return the same offset and a blank record.

I hope you can see that the above steps repeat as fast as your javascript wants, as long as it wants. This works very well for me.

I hope this helps. ;o)

相关阅读:
Top