如何避免在 node.js 中使用模块?
How to avoid using module in node.js?
这样做 tutorial,我不想使用 device.js
只是为了在其中放一行:module.exports = "<a1 ..
如何将此设备 ID 放入我的主脚本中,并将其传递给 apnagent
对象?试过了,但我想这就是连接无法建立的原因。
也许我需要 agent.set('something', and here the device ID);
之类的东西而不是 module.exports = "<388 ..
?
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);
});
你可以试试这个:
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 来导出和导入模块。
这样做 tutorial,我不想使用 device.js
只是为了在其中放一行:module.exports = "<a1 ..
如何将此设备 ID 放入我的主脚本中,并将其传递给 apnagent
对象?试过了,但我想这就是连接无法建立的原因。
也许我需要 agent.set('something', and here the device ID);
之类的东西而不是 module.exports = "<388 ..
?
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);
});
你可以试试这个:
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 来导出和导入模块。