duplicate-requests
Express middleware to avoid duplicates requests made by client
Last updated 6 months ago by romch007 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install duplicate-requests 
SYNC missed versions from official npm registry.

duplicate-requests

This middleware aims to avoid duplicated requests made the client, due to lack of network, or nervous and frustrated endless cliking.

Assuming the client send an unique id with every request (not duplicated requests), the middleware will send an error or resend the content of the previous request if a request already arrived have the same id.

Usage :

import duplicate from "duplicate-requests";
// or
const duplicate = require("duplicate-requests").default;

const express = require("express");
const app = express();

app.use(duplicate({
    expiration: "2h",
    property: "id",
    prefix: "root"
}));

app.get("/", (req, res) => res.end("Hey!"));

app.listen(8080, () => console.log("Listening!"));

Options

{
  expiration: "2h", /* Expiration time of the request in memory
                     * should be an int followed by ms, s, m, h, d, w,
                     */
  property: "id", /* Property which contains the id
                   * should be a string or a function 
                   * with a req paramater which returns a string
                   */
  prefix: "article.add", // Prefix to group requests in storage
  errorHandling: {
    statusCode: 429, // The status code to send if request is duplicated
    json: {} // Javascript plain object to send if request is duplicated
  },
  connectionUri: "" // Leave empty to store object in memory, or use redis:// or mongodb://
}

External storage

If you want to use an external storage (currently supported are Redis and MongoDB), you need to install one of the following package :

npm install --save @keyv/mongo
npm install --save @keyv/redis

Todo

  • [x] Custom error

  • [ ] Parameter to cache request and don't send error

  • [x] Timestamp parser (1d, 3h)

  • [x] Pick id from body

  • [x] Integration test for middleware

Current Tags

  • 1.1.2                                ...           latest (6 months ago)

5 Versions

  • 1.1.2                                ...           6 months ago
  • 1.1.1                                ...           6 months ago
  • 1.1.0                                ...           6 months ago
  • 1.0.1                                ...           6 months ago
  • 1.0.0                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (17)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |