'How to register a url protocol handler in Node.js

I am developing a command line node module and would like to be able to launch it via links on a website.

I want to register a custom protocol my-module:// such that links would have the following format: my-module://action:some-action and clicking on them would start the node package.

If there isn't a node API for this (I'm sure there won't be) then is there a way I can do it from node by invoking system commands?

It must work on Windows, Linux, and MacOS.



Solution 1:[1]

Here's how I did on Mac OS with an application NW.js :

  1. Open the app /Applications/Utilities/Script Editor

    type the following code in the editor

    on open location this_URL
       do shell script "/Applications/X.app/Contents/MacOS/x '" & this_URL & "'"
    end open location
    

    Replace X by the name of your App.

    Save the script as an Application Bundle anywhere

  2. Go to the script, right click then 'Show Package Contents' then edit Contents/info.plist

    Add these lines at the end of the file, just before </dict></plist> :

    <key>CFBundleIdentifier</key>
    <string>com.mycompany.AppleScript.AppName</string> <!-- edit here -->
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
        <key>CFBundleURLName</key>
        <string>AppName</string> <!-- edit here -->
        <key>CFBundleURLSchemes</key>
        <array>
          <string>myurlscheme</string> <!-- your url scheme here -->
        </array>
      </dict>
    </array>
    
  3. You can now open a link starting with myurlscheme: and see your app is opening!

Solution 2:[2]

Edit :

Looks like the module has changed the registration process for good:

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');
});

Original Answer :

There is an npm module for this purpose.

link :https://www.npmjs.com/package/protocol-registry

So to do this in nodejs you just need to run the code below:

First Install it

npm i protocol-registry

Then use the code below to register you entry file.

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');
});

Then suppose someone opens testproto://test then a new terminal will be launched executing :

node yourapp/index.js testproto://test

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Freez
Solution 2