VocoJax
VocoJax

Reputation: 1549

Modifying HTML Website from Node.js Server to Display File Data

So. I'm a total noob. And most of my question is geared towards just not understanding data transfer or Web servers properly.

Anyway, I am creating a project which hosts a very simple and lightweight Node.js server. In this server, I need to create a website to be able to view file data which resides on the server. On the server, I have a directory listing that goes something like this...

- info
--- client_IPAddress1
------ dataFile1.info
------ dataFile2.info
------ dataFile3.info
--client_IPAddress2
------ dataFile1.info
------ dataFile2.info
------ dataFile3.info
--client_IPAddress3
------ dataFile1.info
------ dataFile2.info
------ dataFile3.info

On the node server, I need to be able to read this file data, and modifying a table element on an html page to display the contents of the files. So far, all I know how to do is display the html page directly like this...

if(request.method == 'GET' && request.url == '/'){
		response.writeHead(200, {"Context-Type": "text/html"});
		fs.createReadStream("clientMonitor.html").pipe(response);

Now, I have a button like so...

<div class="refresh-btn" href="http://169.254.0.4:8888/client-data">
        <p><b>Refresh</b></p>
    </div>

Which I want to be able to refresh that data to display to a table element. So, on the node.js server what do I do here?

}else if(request.method == 'GET' && request.url == "/client-data"){
          //TODO
}

I am a complete and total stranger to node.js. A quick solution or a link to a helpful website, or simple a link to a tutorial about data handling that covers topics akin to these would be extremely helpful. I am running this on a Raspberry Pi using Rasbian Jessie. If anyone knows another solution that they could point me for this platform, again, it would help.

Upvotes: 0

Views: 1939

Answers (1)

maxleb
maxleb

Reputation: 50

Not sure what the output needs to be following your example, but I would surly start by using express.js in your project. This will simplify greatly your RESTfull api.

Depending on how noob, you are to node, I will try to guide you thru the simplest way I can! ;)

You first need to install Express in your node project.

From the command shell you type npm install express --save to install the node_module. The --save adds the dependency to your package.json. (Depending on your user's role, you might have to use the sudo command (sudo npm install express --save)

Using the following link Express "Hello World" example you will find the simplest example on how to use Express.

So if we use your example your setting might look like this.

var express = require('express')
var app = express()

app.get('/', function(req, res){
    res.sendFile(__dirname + '/clientMonitor.html');
});

app.get('/client-data', function(req, res){

    /* Depending on what you use on your client side, 
      you may get data here than send a response in json or flat data.*/

    res.json(aJsonObject); // For json response {"Context-Type": "application/json"}

    // OR

    res.send('AnyValue'); // Will send 200, {"Context-Type": "text/html"}

    // OR

    res.sendFile(__dirname + '/someFile.html');

});

app.listen(8080, function () {
  console.log('Example app listening on port 8080!')
})

For your client side, you may use any plugin such as DataTables.net or custom-made function that updates your data in your table using Ajax calls to refresh the information.

Hope this will help solve your issue!

If you decide to use POST instead of get, you will do something like :

app.post('/client-data', function(req, res){    
   var data = req.body.data;
}

Or if you wish to use queryStrings in the Get url parameters, you will do soemthing like :

app.get('/client-data/:id', function(req, res){
   var data = req.params.id;
}

Upvotes: 2

Related Questions