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-temp-links

v0.2.3

Published

An express module to use temporary routes easily

Downloads

5

Readme

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();


// This is an example middleware that checks if the client ip that requested a templink is the same that generated it
const myMiddleware = ( req, res ) => {

    // req.templinks will be defined in temporary link routes
    if ( req.ip === req.templink.refs ) {
        res.send( 'This is a temporary link' );
    } else {
        res.send( 'You are not authorized' );
    }
};


// This instanciates a new set of links that will expire in 10 seconds and call 'myMiddleware' function if a temporary link is requested
const tmpLinks = new TempLinks( { timeOut: 10, callback: myMiddleware } );

// This adds the instance to the selected path (in this example: '/'). You can change parameter name when you instanciate a new links set using 'paramName' option.
app.use( '/:templink', tmpLinks.parser() );

app.get( '/link-generate', ( req, res ) => {

    // This generate a new temporary link with 'GET' method, sets client ip as refs parameter and return the new link string
    const link = tmpLinks.get( { refs: req.ip } );

    // This sends link to the client
    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.refs
  • redirect - Default string that will be passed to res.redirect if it's setted
  • callback - Default middleware callback that will be launched when links are accessed if it's setted
  • paramName - 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 seconds
  • oneTime - Delete link once is accessed
  • method - Any HTTP method you want to use
  • refs - Any refs you want to add to req when the link is accessed
  • redirect - A string that will be passed to res.redirect method when the link is accessed if it's setted
  • callback - 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 previously
  • callback 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();


// This is an example middleware that checks if the client ip that requested a templink is the same that generated it
const tmpMiddleware = ( req, res ) => {

    // req.templinks will be defined in temporary link routes
    if ( req.ip === req.templink.refs ) {
        res.send( 'This is a temporary link' );
    } else {
        res.send( 'You are not authorized' );
    }
};

// This is another example middleware for another links set
const imgMiddleware = ( req, res, next ) => {

    // It deletes non-oneTime links
    req.templink.delete();
    
    // Any action
    
    // It launches next middleware function in the same route of the parser
    next();
};


// This instanciates a new set of links that will expire in 10 seconds and it will call 'tmpMiddleware' function if a temporary link is requested
const tmpLinks = new TempLinks( { timeOut: 10, callback: tmpMiddleware } );

// This instanciates a new set of links that will expire in 5 minutes (by default), it will call 'imgMiddleware' function and links can be accessed many times
const imageLinks = new TempLinks( { oneTime: false, callback: imgMiddleware } );

// This logs any generated links by 'tmpLinks' instance
tmpLinks.on( 'added', ( lnk, obj ) => {
    console.log( lnk );
    console.log( obj.export() );
} );

tmpLinks.on( 'deleted', ( lnk, obj ) => {
    // Other actions...
} );

// These add the instances to the selected paths (in this example: '/' and '/image/'). You can change parameter name when you instanciate a new links set using 'paramName' option.
app.use( '/:templink', tmpLinks.parser() );
app.use( '/image/:templink', imageLinks.parser(), ( req, res, next ) => {
    if ( req.templink ) {
    // If this req is an active templink...

        // It sends 'Hello world'
        res.send( req.templink.refs.join(' ') );
    } else {
        next();
    }
} )


app.get( '/link-generate', ( req, res ) => {

    // This generate a new temporary link, sets client ip as refs parameter and return the new link string
    const link = tmpLinks.get( { refs: req.ip } );

    // This generate a new temporary link, sets a refs parameter and return the new link string
    const imgLink = imageLinks.get( { refs: ['Hello', 'world'] } );

    // These send links to the client
    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 );