Node - Angular Passing POST data from Server to Angular client - javascript

I have a Node.js server with Angular on the client side. I'd like the following to happen.
User posts a large JSON to /postjson. The large JSON object is used in the controller to render some data visualization. I don't want to store the JSON in a database, it should disappear when the window closes. How do I pass this data to the controller?
A potential solution is to use a route /api/postjson and receive the JSON server side. But I don't want to store it. How do I then pass this object to the controller/service/provider whatever is required?

Related

How to mix python and javascript in the server side

I'm creating an application that needs to pull certain data to show it to the user. This data is pulled via a JavaScript function that ideally I'd like to have on the server side, not on the source code of the page.
The flow should be:
User chooses 1 parameter in the website and clicks ok
Send a POST request to Django with that parameter
Django's view uses that parameter to pull another 4 parameters from the Django database
Somehow use this 4 parameters in the JavaScript function to get some timeseries data
Pass this data from JavaScript to the view, and from the view update the browser's template without the user refreshing the page
How can I pass the 4 parameters from Python to the JS function, and then the result of the JS function back to Python?
The reason I need to use JavaScript and not Python for retrieving that data is because JS has a specific library that would make my life so much easier.

Passing sqlite data from Python to the view

I have an sqlite3 database I need to render in the front-end of my application on a GET request. Currently, I am able to render the data as a JSON dump as shown:
if self.path == "/stuff" :
self.send_response(200)
self.send_header("Content-type:", "application/json")
self.wfile.write("\n")
json.dump(db.getAll(), self.wfile)
I have now created a stuff.html where I want to present the data. For example, loop through printing the JSON data to the screen.
Instead of just dumping the data, how would I pass and call the JSON data from the database?
You can directly fetch data from the database using the following link:
access database using javascript from frontend

Is javascript object with data of client save?

I instanciated an (javascript)object which should contain sensible data from our clients(read in via PHP from a database).
My question is now, if this data is save or should I let this run exclusively server-side?

Client-Server AngularJS App - When should I update the model?

I'm working on a client-server application built with angularJS. For data exchanges I'm using HTML5 WebSockets. My server has a dataset that also exists as copy on my client. I refer to this as my model as here is all the raw data.
Via WebSockets I'm able to get/set those data. There may also be random data updates from the server (push).
I'm using different custom services/factories to provide a layer that allows all my modules to create data packets and send them to my server. They also provide wrapper for my model and may change the reprasentation of this data in the view (like timestamps converted to real time, etc.). I have different controllers that access those services to reach for functions or values.
I hope this describes my current architecture good enough. Now my question:
On data updates from the server I update my model data and then inform all modules that make use of this data with the new value. It's like:
Server -> Model -> ViewModel -> View
That works fine and I guess it's ok from the view of the MVVM pattern.
But I'm struggling a bit with publishing updates from the view. At the moment it's like:
View -> ViewModel -> Model (at first update the model)
|
-------> Server (after that send the data to the server)
So, when there's a user updating data in the view, the viewmodel gets updated via angulars data binding. Then the viewmodel calls a service to create an output packet that will be sent to the server. When the packets gets commited my model will be updated with the new data and the packet will be given over to my websocket connection, updating the data on the server.
Is this the/one right way to do so? Is it ok to update my model even before the packet is sent to the server? Should I send the update to the server, wait for an OK and only then update the model?

Sending JSON data to a client? D3.js rendering JSON data

I have a website with a form (currently it's plain HTML but we're switching to JQuery). The flow is like this :
Take in the user's inputs --- 5 integers
Make a call to a web service via REST
Run some calculations server side... and generate a JSON object
??? Now I have my result JSON object, but how do I get it to the client? Persist it?
??? Does the URL need to be the exact location of the JSON file? That would mean 100's
??? of JSON files in my web server's DIR.
D3.js on the client side, waits for the data to be present (via AJAX?).
var data; // a global
d3.json("path/to/file.json", function(error, json) {
if (error) return console.warn(error);
data = json;
visualizeit();
});
Once data is present, render for the client, and remove the calculator from the screen.
I'm having a tough time here, because I notice all AJAX requests need a URL.. but then does that mean I need to give a unique URL to each resulting JSON object? Doesn't that mean I need to persist that object?
I'd just like to have d3.js render my JSON object but unclear what are my options for where to put it.
How do I post parameter on d3.json?
Typically, you will pass some parameters via a javascript object or
a query parameter.
Basically, something like...
data = {}
data.var1 =5;
data.var2 =10;
var my_request = d3.xhr(url)
my_request.post(JSON.stringify(data), function(error,received)){
};
Or
d3.json(url+"?"+"var1=5&var2=10",function(error,received)){
}
Obviously, these form parameters can be parsed on the server easily. After the values are parsed on sever, the server can generate the new JSON using the form parameters that have been parsed.
Make sure that if you are running the script from a local page (or a page that is not on the website) that the server has the ability to allow cross origin (or domain) requests.
If you want to persist the data across multiple calls, you will probably have to to embed the callbacks, or use global variables.

Categories

Resources