I have the server.js file on server side and the index.html on client side. I need to call a function from the server side through maybe a button or something on the index.html and get a feedbak on the index.html that the function was executed.
In the future I would also like to pass over a variable to the server side from client side.
I'm rather new to node.js. Hopefully someone can help me on this.
Node.js is no different than any other server side technology: it's not client side.
If you want to execute something on the server, you have two ways:
Submit a form. This will reload the page and you can choose to redirect on the same page it was.
Run an ajax request from the client side to do something server side.
A third way (thanks to socket.io) is using websockets for your purpose. I wouldn't suggest that you use them if you don't know what it is, though. Learn HTTP, then websockets.
This is a great question. I have done this using Socket.io (http://socket.io/)-- which is a nice library that allows sending data from the client (index.html) to the server, and allowing the server to send data to the client.
Look at the examples on socket.io site.
Well, to make it simple, your function should be "exposed" through some URL. You can use http://expressjs.com/ framework to speed up server side application creation. Once you have that and your function is available at let's say http://localhost/myFunction?myParameter=myValue, you can use jQuery call $.json() to invoke that function from your client side application, i.e. your index.html.
Related
I have a web application with a client that receives data from a server. I have the data in NodeJS, but I want to pass the data to a Javascript file. The Javascript file is included in a HTML file, so I can't make the files communicate with eachother.
I am new to NodeJS, so it can be a stupid question, but anyones help is appreciated
This is for a project where I need have a data stream, and I need to pass it into a web application. I tried to pass the data to different page inside my application and then I tried to get that data on that page inside my web application via Javascript, but I couldn't make that work. I'm not even sure if its possible at this point.
Your node server can't communicate with your front-end without a specific way of communication like websocket, you have many other way to communicate with your front-end as node-server, take a look at server send event for example.
By the way your front-end can call your node server more easely with a get request as said #tomerpacific in comment.
For that you have to open a route with your express app. Routing with express
And for call it on a GET request, for that you can use the XMLHttpRequest, and if you have implemented jQuery on your front, you can use Ajax jQuery.
for example:
If i am using an npm package, i can only use it on server side js. Does that mean one will always have to send data through ajax post/get request for example to server side do the calculation then send data back? Is there a better way to accomplish this?
I am aware of how react works etc and i am aware of workarounds to get require to work browser side, but for sake of understanding workflow and how it should all be set up I ask this question.
I am currently using express with nodejs and using ajax calls to talk to server side js and send info back. So want to know if there is a better way of doing this?
No, there's no "better" way to send data from the client browser to the server than an ajax call. Alternativelly you can use a normal form, or url parameters to pass the server some variable with the page load request.
Depending on the npm package you are using to do your calculations, you can just serve that .js library up along with your .html page by moving that script into a static assets folder and referencing it with a <script> tag. Then, you can do the calculation entirely in the browser (client-side) assuming you don't need any other I/O (db, etc.).
I have a C# server side web service. but I don't want user can to see my requests like request tab from client's browsers.
now, I haven't been find any solution on SO.
what is the best solution to do this?
I think I can use a node.js server-side and render my reactjs inside it and my node.js send my requests to C# server side. like this:
React.js<--(render)--Node.js--(Send/Receive api's)-->C#
I don't know if I use a node.js server, my requests will be hidden from clients?
I don't want to use reactjs.net.
If you're making a HTTP request to node server, and making the stealth request from NodeJS to another server, that request will not be visible to the client.
Alternatively, you can make an encrypted request. Although URL and some part of encryption algorithm will still be exposed at client's end.
Background
I have coded a system that have two parts: server code (ASP.NET MVC + C#) and client side (AngularJS application).
The server side make a calculation of many factors and when it find a "solution" - it's fire a HTTP callback to notify the user.
The client side is presenting a data which found so far by the server. This is the visualization of the data as a user report.
What I have so far?
I currently have both side (server side + client side) coded. The way that HTTP callbacks is catched by the client side (step 3) this is the weak part which I think I should upgrade:
The data is visualize by AngularJS on the browser.
The server side find a solution (zero or more) and fire a HTTP callback to the webserver.
The webserver is getting the HTTP callback and notify the AngularJS app to update its view. This step is made by SignalR which giving me the option to trigger JS code after server event.
The Question
SignalR is giving me the ability to connect the client side (js) to server side events.
Is this possible to update the client side without using SignalR? Of-cause solution like "pull the web-server every N seconds for changes" are not recommended (performance).
Can you think about alternative to SignalR? Alternative system architecture?
Waiting to learn from you the best solution for this problem.
Thank you.
I am a bit confused about how meteor instances server code.
The way I thought it worked, was that when a client connects to the server, the server code is spawned for that session (so variables in the server code will change between users).
However, in testing, it appears that the server side code is one per all of the clients. Is this true? If so, how do I store variables specific to that client?
The answer to this simple question: yes, there is 1 instance of the server code at any given time, thus you should use Session. for your client side variables.