Tuesday, 23 May 2017

Is necessary to define the request in js for connect to an API?

I have never worked with js and now I need a little of help if it was possible. I'm trying to connect to a server using CMX API (Meraki), and I don't know if I need to define the request_uri in my code.

// This script listens for the uri {request_uri}:port/meraki

var listenport = 4567;                                          
var secret = "XXXXX";                                           //Secret that you chose in the Meraki dashboard
var validator = "XXXXXXXXXXX";                      //Validator string that is shown in the Meraki dashboard

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


app.get('/meraki', function(req, res){
  res.send(validator);
  console.log("sending validation")
});


app.post('/meraki', function(req, res){ 
    try {
    var jsoned = JSON.parse(req.body.data);
    if (jsoned.secret == secret) {
       for (i=0; i<jsoned.probing.length; i++) {
          console.log("client " + jsoned.probing[i].client_mac + " seen on ap " + jsoned.probing[i].ap_mac + " with rssi " + jsoned.probing[i].rssi + " at " + jsoned.probing[i].last_seen);
      }
   } else {
       console.log("invalid secret from  " + req.connection.remoteAddress);
   }
} catch (e) {
    // An error has occured, handle it, by e.g. logging it
console.log("Error.  Likely caused by an invalid POST from " + req.connection.remoteAddress + ":");
console.log(e);
res.end();
}

});

app.listen(listenport);
console.log("Meraki presence API receiver listening on port " + 
listenport);

If i don't define the uri which I have to connect, how does the code where to connect? Sorry if the question is stupid.

Thank you.



via Bernat Pedrol Vozmediano

No comments:

Post a Comment