Search code examples
node.jshttpurl-scheme

Can I register a custom URL Scheme/Protocol with a Node HTTP Server?


I would like to be able to handle a custom URL scheme with the Node HTTP API. I would like to write links inside web pages like this: app://foo/bar. I would like to have the Node HTTP Request Handler receive this kind of URL.

When I try this kind of custom protocol in my URL, it looks like Chrome is not sending out the request because it is malformed. So nothing gets to my HTTP server in Node.

Is it possible to bind your HTTP server to a custom URL Scheme or Protocol like app://foo/bar?


Solution

  • Only certain protocols such as http:// and https:// will be sent to your nodejs http server. That's the issue. Your node.js server is an http server. The chrome browser will only send it URLs with the http protocol that it knows belong to an http server.

    A custom protocol has to be first handled in the browser with a browser add-on that can then decide what to do with it.


    Perhaps what you want to do is a custom HTTP URL such as:

    http://yourserver.com/foo/bar
    

    Then, your node.js http server will get the /foo/bar part of the request and you can write custom handlers for that.


    To recap, the first part of the URL the part before the :// is the protocol. That tells the browser what protocol this URL is supposed to be used with. Without a browser add-on, a browser only comes with support for some built-in protocols such as http, https, ws, wss, mailto and some others.

    An http server will only be able to respond to the http protocol so it will only work with URLs that expect to use the http protocol and that the browser knows use the http protocol. Thus your own protocol that the browser does not know about is not something the browser knows what to do with. It would take a browser add-on to tell the browser what to do for a custom URL.

    When I try this kind of url, it almost looks like Chrome is batting it down before it can get to my HTTP server in Node.

    Yes, it's not a recognizable protocol built into the browser so the browser doesn't know what to do with it or how to speak that protocol.

    Is it possible to bind your HTTP server to a custom URL Scheme like this?

    Only with a browser add-on that registers and implements support for the custom URL protocol.