What is the best way for JavaScript to access data from Node.js/

49 views
Skip to first unread message

ABC DEF

unread,
Sep 17, 2016, 7:28:38 PM9/17/16
to nodejs
I'm pretty new to this, and it seems every question is off-topic on stackoverflow, so I'll ask here and if it is still off-topic here, may you direct me to somewhere where it is relevant?

I have a distant Java server that transfers data to a Node socket. I need to manipulate and use this data so that it appears on the webpage in a different form, the manipulation of which can only be done on the client-side JavaScript. Node accesses HTML which accesses JavaScript, so there is a lot of (seemingly needless) depth involved and I'm finding it incredibly difficult to get this data onto the JavaScript side. 

What can JavaScript to to read and clear a file/variable which Node also can? It seems that JavaScript is very limited by what it can do, which makes this problem a heavy challenge for me.

Would the best way to be to set a variable in HTML to "input" and get Node to write the data to it when it is received, and somehow get JavaScript to read that variable and set it blank when it is done with it? Some intuition would be appreciated. Thanks.

Ryan Schmidt

unread,
Sep 18, 2016, 4:47:10 AM9/18/16
to nod...@googlegroups.com
It's hard to know if your question is on-topic because it's very confusing.

Node is a server-side JavaScript runtime. It can be used to run JavaScript code on the server. Often, people use that capability to create a web server. That web server can produce HTML and send it to a client (i.e. web browser) which interprets and displays it the same way it would were it received from any other web server stack (e.g. Apache or Nginx, possibly having been generated by PHP, etc.). Your web server that you create in JavaScript and run with Node can of course send other types of files to the client (web browser) as well, such as images, stylesheets or JavaScript scripts, which the browser will interpret the same way it always does.

The JavaScript language is perfectly powerful, and the Node runtime is a great place to run JavaScript on the server, especially when combined with pre-written npm modules. JavaScript in a web browser is more limited by necessity of the security concerns inherent in running untrusted code. You wouldn't want any random web site you visit to be able to have direct access to the files on your disk, for example, so a web browser doesn't allow JavaScript to do that, but JavaScript in Node doesn't have those limitations.

So, what, specifically, is the data you are sending from your remote Java app to your Node JavaScript app? Can you give an example? And then, in what format does it have to appear on the web page? Can you show any code of what you've done so far, and show us what it currently outputs or does and what you want it to output or do instead?

Zlatko

unread,
Sep 19, 2016, 6:46:36 AM9/19/16
to nodejs
I suspect your last line is the actual question?


On Sunday, September 18, 2016 at 1:28:38 AM UTC+2, ABC DEF wrote:

Would the best way to be to set a variable in HTML to "input" and get Node to write the data to it when it is received, and somehow get JavaScript to read that variable and set it blank when it is done with it? Some intuition would be appreciated. Thanks.

That actually depends on what you want to do.

One way to do this is, fetch data from a "distant Java server" with Node and prepare a HTML page that your clients (browsers) will consume. Example, to set an input value, you would create:

- a fetch-data.js service, to fetch data from the distant Java server:

import request from 'request';
const distantJavaServerUrl = 'https://distant-java-server.com/api/endpoint-for-your-data-in-invalid-format';

export function fetchData() {
 
return new Promise(resolve, reject) {
    request
('http://www.google.com', function (error, response, body) {
     
if (!error && response.statusCode == 200) {
       
// Assuming this is JSON data:
        resolve
(JSON.parse(body)); // or format it better
     
}
      reject
(new Error('Upstream error.'));
   
});
 
});
}


- a views/my-file.ejs html template (omitting a lot of boilerplate):

<html>
<body>
   
<input value="{{ someCount }}">
</body>
</html>

- a node.js server and handler for requests for this file:

import express from 'express';
import { fetchData } from './fetch-service';
const app = express();
app
.set('view engine', 'ejs');

app
.use(function(req, res) {
   
fetchData()
 
.then(data => res.render('my-file', data))
 
.catch(err => res.status(500).send('Server error.\n'));
});


app
.listen(8080, () => console.log('Listening on 8080'));

You can then get your HTML with data here:
[zlatko@zlatko-desktop ~/tmp]$ curl localhost:8080
<html>

<body>
    <input value="12345678">
</
body>
</html>
[zlatko@zlatko-desktop ~/tmp]$


Now, by playing more with your fetch-service.js, you could transform the data in the way you like it. Playing more with the .ejs file, you can format the input, you can name it, add buttons and styles and javascript and other things that make a web page a web page.

Now, how to "read and clear a file/variable which Node also can" is a valid question, too, but it's more of a frontend question better answered on some frontend group.

Now, this is all very basic because you're not asking a specific question. So although JavaScript and Node.js are not very limited in what they can do, it might be challenging if you don't know what you want to achieve. So I would recommend studying a Node.js application design, maybe by following a good book, a video tutorial or other resource would help overcome that challenge.


Reply all
Reply to author
Forward
0 new messages