express-temp-links
express-temp-links is an express module to use temporary routes easily
READ THIS BEFORE USE: since this module stores links in memory the links will become inaccessible after a server restart but you can use import
and export
methods to save links in a database using JSON format
Installation
npm i express-temp-links
Simple usage
const express = require( 'express' );
const TempLinks = require( 'express-temp-links' );
const app = express();
const myMiddleware = ( req, res ) => {
if ( req.ip === req.templink.refs ) {
res.send( 'This is a temporary link' );
} else {
res.send( 'You are not authorized' );
}
};
const tmpLinks = new TempLinks( { timeOut: 10, callback: myMiddleware } );
app.use( '/:templink', tmpLinks.parser() );
app.get( '/link-generate', ( req, res ) => {
const link = tmpLinks.get( { refs: req.ip } );
res.send( `<a href="http://localhost:3000/${link}">http://localhost:3000/${link}</a>` );
} );
app.listen( 3000 );
Methods
TempLinks.constructor
const tmpLinks = new TempLinks( options: Object );
options
timeOut
- Link expiration in seconds (Default: 300)inteval
- Link expiration checking in milliseconds (Default: 1000)oneTime
- It sets if links will be deleted once accessed (Default: true)method
- It sets the default HTTP method (Default: undefined)refs
- It sets any data that can be accessed from req.templink.refsredirect
- Default string that will be passed to res.redirect if it's settedcallback
- Default middleware callback that will be launched when links are accessed if it's settedparamName
- The parameter name in express query routing (Default: 'templink')
Return
A new TempLinks
instance that extends EventEmitter
class, emits an added
event when a link is created and passes that
TempLinks.add
tmpLinks.add( options: Object );
options
timeOut
- Link timeout in secondsoneTime
- Delete link once is accessedmethod
- Any HTTP method you want to userefs
- Any refs you want to add to req when the link is accessedredirect
- A string that will be passed to res.redirect method when the link is accessed if it's settedcallback
- A middleware callback you want to launch when the link is accessed if it's setted
Return
A new temporary link as a string
TempLinks.get
tmpLinks.get( options: Object );
options
The same of add
method, except for method
Return
A new temporary link as a string
TempLinks.post
tmpLinks.post( options: Object );
Parameters
The same of add
method, except for method
Return
A new temporary link as a string
TempLinks.export
tmpLinks.export()
Return
The active links as a JSONable Object
TempLinks.import
tmpLinks.import( links: Object [, callback: Function] );
Parameters
links
Required - A set of links that was exported previouslycallback
Optional - A middleware callback you want to associate to imported links
Advanced usage
const express = require( 'express' );
const TempLinks = require( 'express-temp-links' );
const app = express();
const tmpMiddleware = ( req, res ) => {
if ( req.ip === req.templink.refs ) {
res.send( 'This is a temporary link' );
} else {
res.send( 'You are not authorized' );
}
};
const imgMiddleware = ( req, res, next ) => {
next();
};
const tmpLinks = new TempLinks( { timeOut: 10, callback: tmpMiddleware } );
const imageLinks = new TempLinks( { oneTime: false, callback: imgMiddleware } );
tmpLinks.on( 'added', link => {
console.log( link );
} );
app.use( '/:templink', tmpLinks.parser() );
app.use( '/image/:templink', imageLinks.parser(), ( req, res, next ) => {
if ( req.templink ) {
} else {
next();
}
} )
app.get( '/link-generate', ( req, res ) => {
const link = tmpLinks.get( { refs: req.ip } );
const imgLink = imageLinks.get( { refs: 'example' } );
res.send( `<a href="http://localhost:3000/${link}">http://localhost:3000/${link}</a>` );
res.send( `<a href="http://localhost:3000/${imgLink}">http://localhost:3000/${imgLink}</a>` );
} );
app.listen( 3000 );