I'm building an app with Angularjs with its data stored on json files, so basically i just $http them and have them sorted into the templates.
It was perfectly fine when every .json was ~2kB, now it's over 20kB, so i'm thinking of separating the various parts of the json files into smaller chucks so that every chunk would be exactly what the client needs to receive in order to view a single page in the app (or state) and nothing more. But i want to keep the files as they are, if that's possible.
So, this my question then - is it possible to query json files at the back-end and send to the client only the queried part of it? And if so, how?
Thanks
the only way that I can think of is to use a server side code for that.
For example if we use PHP on the server side it would be something like this:
$string = file_get_contents("file.json");
$json_a = json_decode($string, true);
// server side pagination
The do some classic server side pagination.
Related
I'm pretty freshly learning JavaScript, Node.JS and HTML. Planning to use ExpressJS to display content as follows:
`var app = express();
app.use(express.static(__dirname + '/public'))`
I want to have JSON formatted Data sent to the HTML page and want the user to be able to do requests to an API (that returns more JSON-Formatted Data, yay) and have the results displayed - if possible without reloading the page.
As I said I'm pretty new to this - so any resources or hints would be very appreciated.
On a sidenote, I am trying to stay at NodeJS because I want to access a MySQL database later on.
This is a very general question, but here is some information to get you started in the right direction.
Planning to use ExpressJS to display content as follows:
var app = express();
app.use(express.static(__dirname + '/public'))
Okay, but this doesn't explain how you are going to serve content. If you are just using Express to serve static documents, then this will work. It may be worth your time to get started using a view engine such as EJS or Jade. EJS is usually easier to learn because there is no special syntax, you just wrap plain javascript in <% %> tags. There are a multitude of tutorials on using both of these view engines.
I want to have JSON formatted Data sent to the HTML page
If you are sending static content, you need do nothing more than make a fetch request or xhr request. I recommend getting familiar with fetch first, as it is generally easier to work with than xhr.
With the promise that is returned from that fetch request, you can have some javascript loop through the information and create HTML elements using document.createElement() and insertBefore(). Using this method will not require a page refresh.
Of course, this is just how you would do it with Vanilla JS, and for something like this, there is no need to go bigger. But eventually it becomes easier to use a library like React to create an SPA.
I want to access a MySQL database later on.
Learning how to render json into a page now will likely simplify this step later on, since the Node Mysql driver returns a plain javascript object as a result of its queries, which can easily be sent to the client using something along the lines of res.send(JSON.stringify(results).
There are a multitude of tutorials and github repos on these topics. I recommend doing a little bit of research before diving in, but it all comes down to how much time you spend writing code and debugging.
I'm working on a simple ruby script with cli that will allow me to browse certain statistics inside the terminal.
I'm using API from the following website: https://worldcup.sfg.io/matches
require 'httparty'
url = "https://worldcup.sfg.io/matches"
response = HTTParty.get(url)
I have to goals in mind. First is to somehow save the JSON response (I'm not using a database) so I can avoid unnecessary requests. Second is to check if the new data is available, and if it is, to override the previously saved response.
What's the best way to go about this?
... with cli ...
So caching in memory is likely not available to you. In this case you can save the response to a file on disk.
Second is to check if the new data is available, and if it is, to override the previously saved response.
The thing is, how can you check if new data is available without doing a request for the data? Not possible (given the information you provided). So you can simply keep fetching data every 5 minutes or so and updating your local file.
Hopefully this is simple issues, where I have obviously missed something in the RTFM.
I have an application I am integrating Fine Uploader, and I have it working now in terms of uploading files to the server. The only issue is that I need to take some action on the client side each time the user successfully uploads a file.
In short I would like to have a hidden input field with a comma separated list of files which have been successfully been uploaded.
In my JSON response from my server side implementation. I am of course including "success: true". In addition I have a entry called "file: /path/to/savedUpload.file".
So when an upload is performed successfully, I just need to know how to call my own method with the json response passed in so I can take care of managing the hidden input element.
Thanks in advance for any assistance!
Dustin
I am fairly new to JSON and I want to create a choropleth example as so. http://gabrielflor.it/a-half-decade-of-rising-poverty Whenever the years are clicked it just goes to a different portion of the JSON (I'm assuming). Is this how functionality like this is usually done to avoid redrawing the whole map again and calling another JSON.js file? If so these .JSON files can get quite large?
Using a JSON is only a way to store values you need for each year. When you switch to another year the JS parse the JSON for the giving year and update the choropleth. For the example you have provided, here is the JSON used:
http://gabrielflor.it/static/data/saipe.json
This is a good way since you only have one JSON with every year you need and you load it only once. However since d3 needs datas this way I think you should add another JSON if you want to provide additional data like in gabrielflor example:
http://gabrielflor.it/static/js/d3.poverty-by-county.js?v=121107
He loads JSON like this with d3:
d3.json('../static/data/states.json', function (json) {
states = json;
});
or
d3.json('../static/data/saipehighlights.json', function (json) {
saipehighlights = json;
});
If you look at the network traffic for the example page you gave (ex. by using Chrome Developer Tools).
The file with the poverty data is quite large, but the mapping data file is even larger. You'll notice, that it takes longer for the website to load, but afterwords it runs very smoothly in the client without making any server calls.
The site is just about browsing information and nice design - for that purpose I think a longer load time is quite acceptable if the user experience after is smoother(i.e. user doesn't have to wait for year data to load).
I am creating a dashboard application in which i show information about the servers. I have a Servlet called "poller.java" that will collect information from the servers and send it back to a client.jsp file. In the client.jsp , i make AJAX calls every 2 minutes to call the poller.java servlet in order to get information about the servers.
The client.jsp file shows information in the form of a table like
server1 info
server 2 info
Now, i want to add one more functionality. when the user clicks on the server1, I should show a separate page (call it server1.jsp) containing the time stamps in which the AJAX call was made by calling.jsp and the server information that was retrieved. This information is available in my calling.jsp page. But, how do i show it in the next page.
Initially, i thought of writing to a file and then retrieving it in my server1.jsp file. But, I dont think it is a good approach. I am sure i am missing a much simpler way to do this. Can someone help me ?
You should name your servlet Poller.java not poller.java. Classes should always start with an uppercase. You can implement your servlet to forward to a different page for example if sombody clicks to server1 then the servlet will forward to server1.jsp. Have a look at RequestDispatcher for this. Passing information between request's should be done by request attributes. if you need to retain the information over several request you could think about using session.
In the .NET world, we use SessionState to maintain data that must persist between requests. Surely there's something similar for JSP? (The session object, perhaps.)
If you can't use session state in a servelet, you're going to have to fall back on a physical backing store. I'd use a database, or a known standard file format (like XML). Avoid home-brew file formats that require you to write your own parser.