1

This is my index.html code

<a href= /aoff > <button type="submit" >Force OFF</button></a>
<a href= /aon > <button type="submit" >Force ON </button></a>

and this is my server.js code

var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var system = require('child_process').exec;
var mraa = require('mraa');

var dev_1_status = "OFF";

var content = fs.readFileSync('index.html', 'utf-8');
var compiled = ejs.compile(content);


http.createServer(function(req,res) {
      var temp = 5;
        if(req.url === "/aon") {
            //pin_1.write(0);
                dev_1_status = "ON";
            console.log("pin 2 on");
        }
        if(req.url === "/aoff") {
            //pin_1.write(1);
                dev_1_status = "OFF";
            console.log("pin 2 off");
        }
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(compiled({temp: temp, dev_1_status:dev_1_status}));
}).listen(8080);

I just wanna use AJAX to route to /aoff or /aon without refresh the page, Any help or tutorial on using AJAX with node would be helpful.

Hat hout
  • 471
  • 1
  • 9
  • 18
  • Aside from the option to use isomorphic JavaScript, there isn't much difference between using Node for your backend and anything else as your backend when you use Ajax. It is still just HTTP as far as the browser is concerned. Writing a complete Ajax tutorial would be too broad for Stackoverflow. Writing a complete guide to using the history API, likewise. Both of them together? Along with everything else you'd need, massively too broad. – Quentin Feb 03 '16 at 22:50
  • 1
    Possible duplicate of [Ajax tutorial for post and get](http://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get) – Gavriel Feb 03 '16 at 22:52

0 Answers0