如何使用node / express缓存GET方法?

时间:2019-06-26 19:01:12

标签: node.js typescript api caching web-scraping

我想知道如何为每个GET方法实现一个缓存系统。 ?

我的问题是我有一段动漫视频,当我尝试观看一个章节时,他向我展示了我所看的上一章的视频。这就是为什么我要删除以前的缓存并进行更新,以便向我显示当前请求数据的内容的原因。

任何帮助,我都会感谢你。

import * as scraper from './scraper';
import * as express from 'express';
const router = express.Router();


router.get('/search/:title', (req: express.Request, res: express.Response) => {
  const title: string = req.params.title;
  scraper.searchAnime(title)
    .then(animes => {
      res.status(200).json({
        animes
      });
    }).catch((err) =>{
      console.log(err)
    });
});

//This is GET method that I bring the src of the video.
router.get('/anime/:id/:chapter', (req: express.Request, res: express.Response) => {
  const id: string = req.params.id;
  const chapter: number = req.params.chapter;
  scraper.getAnimeVideo(id, chapter)
    .then(video => {
      res.status(200).json({
        video
      });
    }).catch((err) =>{
      console.log(err)
    });
});

router.get('/letter/:letter/:page', (req: express.Request, res: express.Response) => {
  const letter: string = req.params.letter.toUpperCase();
  const page: number = req.params.page;
  scraper.getAnimesListByLetter(letter , page)
    .then(animes => {
      res.status(200).json({
        animes
      });
    }).catch((err) =>{
      console.log(err);
    });
});


router.get('/latest', (req: express.Request, res: express.Response) => {
  scraper.lastAnimesAdded()
    .then(animes => {
      res.status(200).json({
        animes
      });
    }).then((err) =>{
      console.log(err)
    });
});


export default router;

0 个答案:

没有答案