2016-11-16 47 views
1

這樣做tutorial,我不希望使用device.js只是把一行在它:module.exports = "<a1 ..如何避免在node.js中使用模塊?

enter image description here

怎麼可以把這個設備ID在我的主腳本,並把它傳遞給apnagent對象?試過這個,但我想這就是爲什麼連接沒有建立。

也許而不是module.exports = "<388 ..我需要類似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); 
}); 
+1

也許有幫助:[Node.js module.exports的用途是什麼?你如何使用它?](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node -js-module-exports-how-do-you-use-it) –

回答

1

你可以試試這個:

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); 
}); 

的解釋是這樣的:module.exports是通用模塊輸出的語法。通過這種方式,您可以避免導出模塊並通過在代碼中手動硬編碼deviceId來導入模塊。

+0

我應該使用'deviceId ='還是'device =',因爲腳本最初叫'device.js' ?! –

+0

它是一個變量的名稱;你可以使用你想要的名字。 – pinturic

相關問題