nodejs http module

31 views
Skip to first unread message

Andrew Morgan

unread,
Jan 10, 2018, 9:34:22 PM1/10/18
to nodejs
My goal is to display a website which gives users a menu, then from there they click on what functionality they want to run, and then node runs a command on the back end on a ubuntu server using "exec" then displaying the results to the webpage. So far I have two files, a nodejs file and an html page

var http = require('http'),
   
exec = require('child_process').exec,
    fs
= require('fs')  ;




function onRequest(req, res) {
   res
.writeHead(200, {'Content-Type': 'text/html'});
   fs
.readFile('form.html', function(err, data){
               
if(err)
                       
{
                        res
.write("err.message");
                       
}
                               
else
                               
{
                            res
.write(data);
                            res
.end();
                               
}
         
});




}


var server = http.createServer(onRequest);
server
.listen(80, '0.0.0.0');


console
.log("Server Running");



My goal is to display a website which gives users a menu, then from there they click on what functionality they want to run, and then node runs a command on the back end on a ubuntu server using "exec" then displaying the results to the webpage. So far I have two files, a nodejs file and an html page

var http = require('http'),
    exec = require('child_process').exec,
    fs = require('fs')  ;


function onRequest(req, res) {
   res.writeHead(200, {'Content-Type': 'text/html'});
   fs.readFile('form.html', function(err, data){
                if(err)
                        {
                        res.write("err.message");
                        }
                                else
                                {
                            res.write(data);
                            res.end();
                                }
         });


}

var server = http.createServer(onRequest);
server.listen(80, '0.0.0.0');

console.log("Server Running");

then the form.html







<!DOCTYPE html>

<html>

<body>

<form>

Please select from the following options:

<select id="mySelect">

 
<option value="apps">Insert all Apps belonging to a group</option>

 
<option value="groups">Insert groups in databse</option>

 
<option value="database">Refresh database group table</option>

 
<option value="another">Add all app from one group to another</option>

 
<option value="id">Get Group ID</option>

 
<option value="user">Get User ID</option>

 
<option value="list">Get list of all apps</option>

</select>

</form>




<p>Click the button to change the selected fruit to banana.</p>




<button type="button" onclick="myFunction()">Try it</button>




<script>

function myFunction() {




 
if(document.getElementById("mySelect").value = "apps")

   
{

     
//do something and display results to webpage

     
}

   
else

   
{

   
//display error

   
}




}

</script>




</body>

</html>


My question is, is this the right way of carrying out this task? and if so,should I export the onRequest function which would then allow me to display what I want to the webpage with res.write ?
Reply all
Reply to author
Forward
0 new messages