[英]How to get utility function from helper file on node.js server?
我有一個節點/表達服務器,我正在嘗試從幫助程序文件中獲取一個功能到我的app.js中供使用。 這是幫助文件中的函數:
CC.CURRENT.unpack = function(value) { var valuesArray = value.split("~"); var valuesArrayLenght = valuesArray.length; var mask = valuesArray[valuesArrayLenght-1]; var maskInt = parseInt(mask,16); var unpackedCurrent = {}; var currentField = 0; for(var property in this.FIELDS) { if(this.FIELDS[property] === 0) { unpackedCurrent[property] = valuesArray[currentField]; currentField++; } else if(maskInt&this.FIELDS[property]) { //i know this is a hack, for cccagg, future code please don't hate me:(, i did this to avoid //subscribing to trades as well in order to show the last market if(property === 'LASTMARKET'){ unpackedCurrent[property] = valuesArray[currentField]; }else{ unpackedCurrent[property] = parseFloat(valuesArray[currentField]); } currentField++; } } return unpackedCurrent; };
在該幫助程序文件的底部,我做了一個module.export(幫助程序文件長400行,我不想導出其中的每個函數):
module.exports = { unpackMessage: function(value) { CCC.CURRENT.unpack(value); } }
然后在我的app.js中
var helperUtil = require('./helpers/ccc-streamer-utilities.js');
最后,我在app.js和console.log中調用了該函數:
res = helperUtil.unpackMessage(message); console.log(res);
問題是console.log每次都會發出未定義的消息,但是在此示例中: https : //github.com/cryptoqween/cryptoqween.github.io/tree/master/streamer/current (不是node.js) ),效果很好。 所以我認為我輸入錯誤。 我要做的就是在我的app.js中使用該實用程序功能
unPackMessage(val)
調用不返回任何內容:
module.exports = {
unpackMessage: function(value) {
CCC.CURRENT.unpack(value);
}
}
您需要返回CCC.CURRENT.UNPACK(value);
module.exports = {
unpackMessage: function(value) {
return CCC.CURRENT.unpack(value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.