我正在开发一个命令行节点模块,并希望能够通过网站上的链接启动它。
我想注册一个自定义协议my-module://
,这样链接将具有以下格式:my-module://action:some-action
,单击它们将启动节点包。
如果没有用于此操作的节点API (我确信不会有),那么有没有一种方法可以通过调用系统命令从节点执行此操作?
它必须能在Windows、Linux和MacOS上运行。
发布于 2021-04-23 22:36:02
编辑:
看起来模块已经永久地改变了注册过程:
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // sets protocol for your command , testproto://**
command: `node ${path.join(__dirname, './tester.js')} $_URL_`, // this will be executed with a extra argument %url from which it was initiated
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true, // Use this to run your command inside a terminal
script: false
}).then(async () => {
console.log('Successfully registered');
});
原始答案:
有一个用于此目的的npm模块。
链接:https://www.npmjs.com/package/protocol-registry
因此,要在nodejs中执行此操作,您只需运行以下代码:
首先安装它
npm i protocol-registry
然后使用下面的代码注册您的入口文件。
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // set your app for testproto://**
command: `node ${path.join(__dirname, './index.js')}`, // this will be executed with a extra argument %url from which it was initiated
}).then(async () => {
console.log('Successfully registered');
});
然后假设有人打开testproto://test,然后执行以下命令启动一个新的终端:
node yourapp/index.js testproto://test
发布于 2013-08-30 22:14:09
首先,创建自己的协议是疯狂的,不要这么做。
看起来你想要做的就是能够从web浏览器启动你的node包。从web浏览器启动外部程序的通常方法是编写浏览器插件。
这就是如何从你的浏览器启动pdf,torrents,iTunes。
因此,总而言之,要做你想做的事情,你必须编写一个插件并让你的用户安装。我想说这不是个好主意。
https://stackoverflow.com/questions/18534591
复制相似问题