Skip to main content

How do I use a domain running Node.js/Express to shorten links?

Updated over a month ago
Rebrandly

While it's possible to reroute traffic from your website domain to branded links, we highly recommend using a dedicated domain for your branded links. With a unique custom domain, there is no complicated CMS or server configuration, few possible points of failure, and no dependency on your website staying configured correctly.

If you decide that you don't want a separate domain, Rebrandly's domain alias feature lets you use the same domain for both your website and your links.

You can use a domain from your Express web server for branded short links by setting it up as an alias domain in Rebrandly and installing our package on your server to handle the routing. Please be aware that incorrect changes could bring your website down. This is a technical process that requires the knowledge and permissions to operate on your website or CMS.

Before you begin, make sure you have:

  • A Rebrandly account

  • A domain you own

  • An Express web server with Node.js installed

  • Access to modify your server code

To integrate Rebrandly aliasing in your Express web server, you will need to:

Add the domain to your Rebrandly account

  1. Log in to Rebrandly.

  2. Click Domains.

    nav_domains.png
  3. Click New Domain.

    domains_new-domain 2.png
  4. Click Connect a domain you already own.

    to domain or not to domain.png
  5. Enter the domain name.

  6. Select No, it's being used for my website.

  7. Click Use domain directly.

  8. Click Save.

  9. Copy the alias domain. Keep this page open, as you'll need this information for your code.


    You can also find it later from your Domains list by clicking Server Configuration.

Install the Rebrandly Express package

  1. Install the rebrandly-express npm package from the repository: https://github.com/rebrandly/rebrandly-express

  2. Add the the package to your Express application. Replace ##{{alias}} with your domain name from step 9 above.

    const rebrandlyRouter = require("rebrandly-express");
    const options = { "alias": "example.com" }; // Replace with your alias domain
    const fallbackToRebrandly = rebrandlyRouter(options);// Initialize your app as usual and add your own middlewares
    ...
    // Add Rebrandly right before your catchall rule for 404
    app.use(fallbackToRebrandly);
    app.use('*', your404Handler);
  3. Restart your Express server.

For detailed configuration options and troubleshooting, see the rebrandly-express documentation on GitHub.

Test the integration

Create a branded link in Rebrandly using your alias domain, then open the short link in your browser. If everything is configured correctly, it should redirect to your destination URL.

Did this answer your question?