0

i am writing a code for display the scan result on web server via http but is always gives me errr any one can solve this issue here is my code

var http = require('http');    
var wifi = require('node-wifi');

wifi.init({    
    debug : true,    
    iface: 'wlan0'    
});     

http.createServer(function (req, res) {    
    res.writeHead(200, {'Content-Type': 'application/json'});    
    var otherArray = ["item1", "item2"];    
    var otherObject = { item1: "item1val", item2: "item2val" };    
    var json = JSON.stringify({     
       anObject: otherObject,     
       anArray: otherArray,     
       another: "item"    
      });    
    wifi.scan(function(networks){    
        console.log(networks);    
    });    
    res.end(json,networks);    
}).listen(1337, '127.0.0.1');    
console.log('Server running at http://127.0.0.1:1337/');
lokusking
  • 7,396
  • 13
  • 38
  • 57

1 Answers1

0

Don't know why I'am answering, cause I think your code is completely copied from these posts:

But here you go. Try this:

var http = require('http');
var wifi = require('node-wifi');

wifi.init({
    debug : true,
    iface: 'wlan0'
});

// listen on port 1337
http.createServer((req, res) => {
    // set response header
    res.writeHead(200, {
        'Content-Type': 'application/json'
    });

    // scan for wifi networks
    wifi.scan((err, networks) => {
        // check if there is an error
        if (err) {
            res.end(`{"error":"${err}"}`);
        } 
        // stringify your network object and give it back to the client
        else {
            res.end(JSON.stringify(networks));
        }
    });
}).listen(1337);

console.log('Server running at http://127.0.0.1:1337/');

You can also type this into your cli:

  • Get all network devices: arp -a
  • Filter network devices: arp -a | grep en0
Community
  • 1
  • 1
Philip
  • 3,486
  • 1
  • 24
  • 37
  • It works bro now i just want to how i can use this file in my Ionic framework –  Jan 19 '17 at 05:11
  • This is an other question. stackoverflow is not a place to let people write code for you. If this is the correct answer, mark it. Anyway ... You have to do an AJAX-Call to `http://127.0.0.1:1337/` with the help of angular `$http`. https://docs.angularjs.org/api/ng/service/$http – Philip Jan 19 '17 at 08:50