我编写了以下API包装器,并希望在快速视图中输出它。
我的API调用文件是exchanges.js,我使用以下函数来getQuotes
:
module.exports = function container(get, set, clear) {
function publicClient() {
if (!public_client) {
public_client = new KrakenClient()
}
return public_client
}
var exchange = {
name: 'kraken',
historyScan: 'forward',
makerFee: 0.16,
takerFee: 0.26,
// The limit for the public API is not documented, 1750 ms between getTrades in backfilling seems to do the trick to omit warning messages.
backfillRateLimit: 1750,
getQuote: function(opts, cb) {
var args = [].slice.call(arguments)
var client = publicClient()
var pair = joinProduct(opts.product_id)
client.api('Ticker', {
pair: pair
}, function(error, data) {
if (error) {
if (error.message.match(recoverableErrors)) {
return retry('getQuote', args, error)
}
console.error(('\ngetQuote error:').red)
console.error(error)
return cb(error)
}
if (data.error.length) {
return cb(data.error.join(','))
}
cb(null, {
bid: data.result[pair].b[0],
ask: data.result[pair].a[0],
})
})
},
}
return exchange
}
我尝试通过快速路线getQuotes
致电ticker.js
:
var express = require('express');
var router = express.Router();
var kraken = require("../exchanges/kraken/exchange")
router.get('/', function(req, res, next) {
var tick = kraken.exchange.getQuote;
res.send('get ticker: ' + tick);
});
module.exports = router;
但是,在打开路径时,我收到以下错误消息:
无法读取未定义的属性'getQuote'
TypeError:无法读取未定义的属性“getQuote” 在/home/ubuntu/workspace/nodejs/routes/ticker.js:7:31 在Layer.handle [as handle_request](/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/layer.js:95:5) 在下一个(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/route.js:137:13) 在Route.dispatch(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/route.js:112:3) 在Layer.handle [as handle_request](/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/layer.js:95:5) at /home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:281:22 在Function.process_params(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:275:10) 在Function.handle(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:174:3) 在路由器(/home/ubuntu/workspace/nodejs/node_modules/express/lib/router/index.js:47:12)
有关如何正确调用getQuotes
变量的任何建议吗?
感谢您的回复!
答案 0 :(得分:1)
要导入并使用getQuote
,您需要将其包含在内var kraken = require("./kraken")()
然后您就可以致电getQuote
购买调用var tick = kraken.getQuote();
这是一个工作示例
var express = require('express');
var app = express();
var kraken = require("./kraken")()
app.get('/', function(req, res, next) {
var tick = kraken.getQuote();
res.send('get ticker: ' + tick);
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
});
编辑:我一直在工作,所以我的路径是./kraken
你还应该使用你的默认路径