friedrith / node-wifi

📶 NodeJS tool to manage wifi (connections, scans)
MIT License
395 stars 161 forks source link

Working when deployed? #61

Closed weiwanghasbeenused closed 5 years ago

weiwanghasbeenused commented 5 years ago

Hi,

I'm new to Node so please forgive me if this is dumb. I have a code that detects wifi signals around me and set up a server for another page to grab it when I ran it locally. However, when I deployed it on Heroku, It just won't work. Is it because of that part of setting up a server or...? I would appreciate if you can spend some time looking at this. Thank you!

` var express = require('express'); var app = express(); var wifi = require('node-wifi'); var parsedNetworks; var cors = require('cors'); var towers0; var ip = require('ip'); var myIP = ip.address(); var https = require('https'); var path = require('path'); var fs = require('fs');

console.log(myIP); // my ip address

/wifi/ app.use(cors())

wifi.init({ iface :null }); wifi.scan(function(err, networks){ if(err){ console.log(err); }else{ parsedNetworks = []; towers0 = []; networks.forEach(function(e){ parsedNetworks.push({'ssid': e.ssid, 'signal_level':e.signal_level, 'mac':e.mac, 'security':e.security}) }) for(i=0;i<networks.length;i++){ console.log(networks[i].ssid); } } });

app.get('/getwifis', function (req, res) { res.json(parsedNetworks); }) var certOptions = { key: fs.readFileSync(path.resolve('server.key')), cert: fs.readFileSync(path.resolve('server.crt')) }

var server = https.createServer(certOptions, app).listen(8081,function () { var host = server.address().address var port = server.address().port

console.log("Example app listening at https://%s:%s", host, port) }) `

Wei

friedrith commented 5 years ago

Hi,

Can you format your code using code balise in markdown.

Node-wifi is not meant to be used on server since most of the time servers don't have any wifi card. And you cannot used node-wifi in the broswer since it uses child-process from node api.

weiwanghasbeenused commented 5 years ago

I see...I guess I need to find another library. Thank you though!

Sorry about the messy code. Here's the formatted one if you're interested. Some CS student helped me with it so I'm not 100% sure about how it works.

var express = require('express');
var app = express();
var wifi = require('node-wifi');
var parsedNetworks;
var cors = require('cors');
var ip = require('ip');
var myIP = ip.address();
var https = require('https');
var path = require('path');
var fs = require('fs');

console.log(myIP); 
app.use(cors())

/*wifi*/
wifi.init({
    iface :null
});
wifi.scan(function(err, networks){
    if(err){
        console.log(err);
    }else{
        // pushing data to array "parsedNetworks"
        parsedNetworks = [];
        networks.forEach(function(e){
            parsedNetworks.push({'ssid': e.ssid, 'signal_level':e.signal_level, 'mac':e.mac, 'security':e.security})
        })
        // consoling ssid of each signal
        for(i=0;i<networks.length;i++){
          console.log(networks[i].ssid);
        }
    }
});
// sending wifi data at url/getwifis
app.get('/getwifis', function (req, res) {
   res.json(parsedNetworks);
})

// setting up https server
var certOptions = {
  key: fs.readFileSync(path.resolve('server.key')),
  cert: fs.readFileSync(path.resolve('server.crt'))
}

var server = https.createServer(certOptions, app).listen(8081,function () {
   var host = server.address().address
   var port = server.address().port
   console.log("Example app listening at https://%s:%s", host, port)
})
lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.