首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Node.js中注册url协议处理程序

如何在Node.js中注册url协议处理程序
EN

Stack Overflow用户
提问于 2013-08-30 21:51:05
回答 2查看 11.2K关注 0票数 27

我正在开发一个命令行节点模块,并希望能够通过网站上的链接启动它。

我想注册一个自定义协议my-module://,这样链接将具有以下格式:my-module://action:some-action,单击它们将启动节点包。

如果没有用于此操作的节点API (我确信不会有),那么有没有一种方法可以通过调用系统命令从节点执行此操作?

它必须能在Windows、Linux和MacOS上运行。

EN

回答 2

Stack Overflow用户

发布于 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
票数 2
EN

Stack Overflow用户

发布于 2013-08-30 22:14:09

首先,创建自己的协议是疯狂的,不要这么做。

看起来你想要做的就是能够从web浏览器启动你的node包。从web浏览器启动外部程序的通常方法是编写浏览器插件。

这就是如何从你的浏览器启动pdf,torrents,iTunes。

因此,总而言之,要做你想做的事情,你必须编写一个插件并让你的用户安装。我想说这不是个好主意。

票数 -25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18534591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档