本机javascript插件无法使其在浏览器上运行

时间:2020-03-06 23:22:48

标签: javascript node.js browserify

我是Java语言的新手,我试图使用本机插件,并使用控制台下的node对其进行了编译和测试。但是,当它包含在html中并在chrome下运行时,总是说require尚未定义。然后,我使用browserify对其进行了捆绑,但收到了以下消息:

解析错误:意外字符'?'

index.js:

const Input = require('./build/release/sendinput.node');
console.log('test',Input);
function SendInput(inputs){
    if(!Array.isArray(inputs))
        inputs = [inputs];
    let arr = [];
    for(let inp of inputs){
        if(typeof inp != "object")
            throw new Error("Expecting array of objects");
        if(!Number.isInteger(inp.type) || inp.type < 0 || inp.type > 2)
            throw new Error("Expecting type to be an integer from 0 to 2");
        if(!Number.isInteger(inp.val))
            throw new Error("Expecting val to be an integer")
        switch(inp.type){
            case 0:
                arr.push(Input.CreateKBDInpVKey(inp.val, !!inp.up));
            break;
            case 1:
                let val = inp.val;
                let extended = false;
                if(val >> 8 & 0xFF == 0xe0)
                    extended = true;
                arr.push(Input.CreateKBDInpScanCode(inp.val & 0xFF, !!inp.up, extended));
            break;
            case 2:
                arr.push(Input.CreateKBDInpUnicode(inp.val, !!inp.up));
            break;
        }
    }
    return Input.SendInput(arr);
}
module.exports = {SendInput}  //SendInput now a object.

通过运行:node index.js,输出为:

test {
  SendInput: [Function],
  CreateKBDInpVKey: [Function],
  CreateKBDInpScanCode: [Function],
  CreateKBDInpUnicode: [Function]
}

我需要让它们在浏览器上工作。有人可以给我一些帮助吗?谢谢

1 个答案:

答案 0 :(得分:1)

您不能在浏览器上使用本机节点模块,仅此而已。 您可以运行Nodejs休息服务器,并使用浏览器到服务器的rest调用,让Nodejs完成工作并在浏览器上使用响应。