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-http-redis

v1.0.41

Published

[![codecov](https://codecov.io/gh/Joao208/express-http-redis/branch/main/graph/badge.svg?token=50MD3A8XVM)](https://codecov.io/gh/Joao208/express-http-redis) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Joao208_express-ht

Downloads

68

Readme

Express Http Redis

codecov Sonarcloud Status

By using this library your requests time is reduced by 80% The library intercepts the get requests and returns the response that was saved in its cache. For use, you need the host, port, key prefix and password of Redis, and keys (Keys are one string with the path of params for lib used to create key).

Example of using keys

['query.id','params.route'] // key: `${req.query.id} : ${req.params.route}`
['params.id'] // key: `${req.params.id}`
['url'] // key: `${req.url}`

Middleware can be used globally or in just one route, for example The problem with using middleware with global status is that the library cannot access req.params

app.use(middleware)

// or

app.get("/", middleware, (req, res) => {
  return res.json("ok");
})

But how does the construction of keys work? The keys are built in the pattern: parameter1 : parameter2 : parameter3... But you don't need to keep building the key whenever you need to query or create a cache, you can use the createKeyString function exported from the library, passing only the req An example of use

cache.delete(createKeyString(req));

The library also exports all used interfaces, you can access it in the two ways below

import { ICache, IInit, IObj } from "express-http-redis";

// or

import { ICache, IInit, IObj } from "express-http-redis/types";

Inside the cache export there are 3 methods: delete, post, get

cache.post(createKeyString(req), {});

cache.delete(createKeyString(req));

cache.get(createKeyString(req))

The full use of the library looks like this:

import "dotenv/config";

import express from "express";
import "express-async-errors";
import cors from "cors";
import http from "http";
import responseTime from "response-time";
import { cache, Cache, middleware, createKeyString } from "express-http-redis";

const port = 5000;

const app = express();
const server = http.createServer(app);

app.use(cors());
app.use(express.json({}));
app.use(express.urlencoded({ extended: true }));
app.use(
  responseTime((req, res, time) => {
    console.log(`${req.method} ${req.url} ${time}`);
  })
);

new Cache({
  host: process.env.HOST,
  port: process.env.PORT,
  keyPrefix: process.env.KEYPREFIX,
  password: process.env.PASSWORD,
  keys: ["params.id", "query.number"],
});

app.post("/:id", middleware, (req, res) => {
  const users = { id: 10, name: "User" };

  cache.post(createKeyString(req), { data: users });

  return res.json("ok");
});

app.delete("/:id", middleware, (req, res) => {
  cache.delete(createKeyString(req));

  return res.json("ok");
});

app.delete("/", middleware, (req, res) => {
  cache.delete(createKeyString(req));
  return res.json("ok");
});

server.listen(port, () => {
  console.log(`We are live on ${port}`);
  console.log(`Environment: staging`);
});