János János - 23 days ago 7
Javascript Question

How to avoid using module in node.js?

Doing this tutorial, and I do not want to use

device.js
just to put one line in it:
module.exports = "<a1 ..


enter image description here

How could put this device ID in my main script, and pass it to
apnagent
object? Tried this, but I guess this is why connection not establishing.

Maybe instead of
module.exports = "<388 ..
I need something like
agent.set('something', and here the device ID);
?

var pfx = '/var/lib/openshift/555dd1415973ca1660000085/app-root/runtime/repo/pfx.p12';
module.exports = "<38873D3B 0D61A965 C1323D6C 0A9F2866 D1BB50A3 64F199E5 483862A6 7F02049C>"; // <----- THIS HERE

var apnagent = require('apnagent')
var agent = module.exports = new apnagent.Agent();
agent.set('pfx file', pfx);
// our credentials were for development
agent.enable('sandbox');

console.log('LOG1');
console.log(agent);
agent.connect(function (err) {

console.log('LOG2');
// gracefully handle auth problems
if (err && err.name === 'GatewayAuthorizationError') {
console.log('Authentication Error: %s', err.message);
process.exit(1);
}

// handle any other err (not likely)
else if (err) {
throw err;
}

// it worked!
var env = agent.enabled('sandbox')
? 'sandbox'
: 'production';

console.log('apnagent [%s] gateway connected', env);
});

Answer

You can try this:

var pfx = '/var/lib/openshift/555dd1415973ca1660000085/app-root/runtime/repo/pfx.p12';
var deviceId = "<38873D3B 0D61A965 C1323D6C 0A9F2866 D1BB50A3 64F199E5 483862A6 7F02049C>"; // <----- THIS HERE

var apnagent = require('apnagent')
var agent = new apnagent.Agent(deviceId);
agent.set('pfx file', pfx);
// our credentials were for development
agent.enable('sandbox');

console.log('LOG1');
console.log(agent);
agent.connect(function (err) {

console.log('LOG2');
  // gracefully handle auth problems
  if (err && err.name === 'GatewayAuthorizationError') {
    console.log('Authentication Error: %s', err.message);
    process.exit(1);
  }

  // handle any other err (not likely)
  else if (err) {
    throw err;
  }

  // it worked!
  var env = agent.enabled('sandbox')
    ? 'sandbox'
    : 'production';

  console.log('apnagent [%s] gateway connected', env);
});

The explanation is this: module.exports is the common module export syntax. This way you are avoiding exporting a module and importing it by manually hardcoding the deviceId in your code.