shorturl-tsv-express-middleware v0.4.0
ShortURL TSV Express Middleware
Comfortably manage your shortURLs with Google Sheets
Features
Minimal maintenance (small, single NPM dependency) It's 139 simple lines of JavaScript and one dependency to
parseurl
which comes withexpress
.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 headlinedPublish to the web
. - Make sure
Link
is selected (notEmbed
). - Select
Sheet1
(notEntire 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.