Skip to content

reasn/shorturl-tsv-express-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShortURL TSV Express Middleware

Comfortably manage your shortURLs with Google Sheets

Cross-Platform Compatibility License Buy us a tree

Features

  • Minimal maintenance (small, single NPM dependency)
    It's 139 simple lines of JavaScript and one dependency to parseurl which comes with express.

  • Manage your shortURLs with Google Sheets
    No additional persistence needed, set permissions, use formulas and create your own user interfaces.

Installation and Use

Install using npm.

npm install @reasn/shorturl-tsv-express-middleware --save

Then use it in your Node.js script like this:

const express = require('express');
const app = express();  
const createMiddleware = require('./index');

let server;

app.use(createMiddleware({
    url: "URL to your TSV file",
    onError: error => {
        console.error(error);
        server.close();
    }
}));

server = app.listen(process.env.PORT || 3000);

Use Google Sheets as management interface

  • Make sure you have a Google account.
  • Create a new Google Sheet by going to sheet.new.
  • Enter shortUrls in the first columns and the targets to redirect to in the second column.
  • Select File > Publish to the web to open a dialog box headlined Publish to the web.
  • Make sure Link is selected (not Embed).
  • Select Sheet1 (not Entire Document) in the first drop-down.
  • Select Tab-separated values (.tsv) in the second drop-down.
  • Make sure to uncheck Require viewers to sign in ....
  • Make sure to check Automatically republish when changes are made.
  • Copy the url to your newly published file and use it in your script as options.url

Runtime Configuration

Below is an example with all possible options parameters.

app.use(createMiddleware({
    url, // The URL to your TSV file
    onError, // error handler (`error => undfined`)
    request, //request || https.get,
    interval, // Interval to fetch updates, default: 1000 * 60 * 5, i.e. 5 minutes
    preventAutoStart, // Unless set to true, the regular fetching of updates is starts when invoking createMiddleware(), default: false
    updateRoute, // Exposes a route to force updates via GET request. This can be used for a reflected (D)DOS. Check whether that's a threat before using.
    responseCode, // the 3xx HTTP response code, default: 302
    log, // log message handler (`message => undefined`)
    maxOpenRequests, // The number of possible open requests to fetch updates, default: 5
    maxHops, // The number of redirect lookups before assuming a circular redirect, default: 50
    };
}));

server = app.listen(process.env.PORT || 3000);

Contributing

I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.

License

Swagger Express Middleware is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published