npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-serve-public

v1.0.1

Published

Instant Public URLs for Express

Downloads

185

Readme

Instant Public URLs for express

Express Serve Public will generate an instant public URL for your express application. This URL will work behind firewalls and will connect to your local machine via a tunnel. You don't need any special networking config or setup. It will work behind NAT and CG-NAT.

Behind the scenes, this uses Tunnelmole (an open source ngrok alternative) to get the public URLs.

Installation

Add express-serve-public to your project with npm

npm install --save express-serve-public

Usage

First, import the module

First you'll need to import the servePublic module. Both ES and CommonJS style imports are supported.

ES style import:

import { servePublic } from 'express-serve-public`;

CommonJS style import:

const servePublic = require('express-serve-public/cjs');

Serve your express application with a public URL

Here is a full example express application which will generate a randomized public URL at launch

// app.js
import { Request, Response } from "express";
import { servePublic } from "../src/servePublic.js";
import express from "express";

/**
 * An example to get an instant public URL for your express app.
 *
 * Heavily based on the Hello World example from https://expressjs.com/en/starter/hello-world.html
 */
const app = express();

app.get("/", (request, response) => {
    response.send("Hello World!");
});

servePublic(
    app, 
    { 
        port: 3000,
    }, () => {
        console.log(`Example app listening on port 3000`);
    }
);

If your project is using CommonJS, here is a CommonJS example: https://github.com/robbie-cahill/express-serve-public/blob/main/cjs/express-serve-public.js.

When this application runs, you should see output similar to the following:

Example app listening on port 3000
http://kqr4cc-ip-157-211-195-169.tunnelmole.com is forwarding to localhost:3000
https://kqr4cc-ip-157-211-195-169.tunnelmole.com is forwarding to localhost:3000

The express application has started and is listening on port 3000. Tunnelmole has generated a public URL that is pointing to your express application.

Try hitting one of these URLs from any device on the internet and like magic they will connect to your locally running express server.

Well not exactly magic, heres how it works.

Custom subdomains

Sometimes, it can be useful to have a URL that does not change and for that, Tunnelmole supports custom subdomains.

To get a URL that does not change, pass in the domain option to servePublic as follows:

servePublic(
    app, 
    { 
        port: 3000,
        domain: '<yourdomain>.tunnelmole.com`
    }, () => {
        console.log(`Example app listening on port 3000`);
    }
);

If you are using the hosted service (which is the default) and you want to use a custom subdomain you'll need to purchase a subscription Learn More.

Otherwise, you can self host as the service is also open source so nothing prevents you from running your own server, no subscriptions needed. To learn more about this option go to the Tunnelmole Service GitHub repo.

Get a public URL for other things

Tunnelmole is available as a standalone CLI application (npm install -g tunnelmole) and as an NPM dependency for use with other projects such as React for example. Get it from (https://github.com/robbie-cahill/tunnelmole-client).

This project uses the dependency to give you a nice wrapper function to use with express.