【问题标题】:How to clear cache in inversifyJS?如何清除 inversifyJS 中的缓存?
【发布时间】:2022-01-22 10:14:13
【问题描述】:

我有一个简单的应用程序,我使用express 服务器和TypeScript 作为编码语言。起初我可以成功到达终点,但是当我更改模型时,我看到了旧数据。

我也尝试过这种方法,但没有运气,How to disable webpage caching in ExpressJS + NodeJS?。

我在这里错过了什么?

  1. 我的index.ts(主)文件
/**
 * Required External Modules
 */
import "reflect-metadata"
import * as dotenv from "dotenv";
import express from "express";
import cors from "cors";
import helmet from "helmet";
import routes from "./routes/routes";
import compress from 'compression';
const nocache = require('nocache');

dotenv.config();

/**
 * App Variables
 */
if (!process.env.PORT) {
    process.exit(1);
}

const PORT: number = parseInt(process.env.PORT as string, 10);

const app = express();

/**
 *  App Configuration
 */
app.use(helmet());
app.use(cors());
app.use(express.json());
app.disable('x-powered-by'); // Hide information
app.use(compress());
// app.disable('view cache');
// app.use((req, res, next) => {
//     res.set('Cache-Control', 'no-store')
//     next()
// })
app.use(nocache());
routes(app);

/**
 * Server Activation
 */
app.listen(PORT, () => {
    console.log(`Listening on port ${PORT}`);
});
  1. route 声明。
import { Application } from "express";
import asyncWrap from "../utils/asyncWrap";
import HealthController from "../controllers/health.controller";
import container from "../ioc.container";

export default function (application: Application) {
    const healthController = container.get<HealthController>(HealthController);
 
    application.get('/healthStatus', asyncWrap(healthController.healthCheck.bind(healthController)));
}
  1. asycWrap逻辑
    import { 
        Request as ExpressRequest, 
        Response as ExpressResponse, 
        NextFunction, 
    } from 'express';
    
    // Wraps async functions, catching all errors and sending them forward to express error handler
    export default function asyncWrap(controller: CallableFunction) {
        return async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
            try {
               await controller(req, res, next);
            } catch (e) {
                next(e);
            }
        };
    }
  1. controller逻辑
import { inject, injectable } from "inversify";
import { HealthModel } from "../models/health.model";
import HealthService from "../services/health.service";
import { 
    Request as ExpressRequest, 
    Response as ExpressResponse, 
} from 'express';

@injectable()
export default class HealthController {

    constructor(
        @inject("HealthService") private HealthService: HealthService,
    ) { }

    public async healthCheck(req: ExpressRequest, res: ExpressResponse): Promise<HealthModel> {
        const returnThis = await this.HealthService.healthCheck();
        res.send(returnThis);
        return returnThis;
    }
}
  1. service逻辑
import { injectable } from "inversify";
import { HealthModel } from "../models/health.model";

@injectable()
export default class HealthService {

    public async healthCheck(): Promise<HealthModel> {
        const healthModel: HealthModel = {
            dateTime: new Date(),
            description: "Hello WOrld",
            status: "sdfadfad"
        }
        return healthModel;
    }
}
  1. 输出(这是我的旧结果)

【问题讨论】:

标签: node.js typescript express


【解决方案1】:

花了将近一天的时间后,我发现我没有在更改后编译我的.ts 文件(这是我的愚蠢错误)。所以,很明显 compile 不会接受我新添加的更改。

我在package.json 中添加了start 脚本,它将编译我的.ts 文件+ 运行源文件。

这里是package.json 文件:

  • 增强现实
    "scripts": {
        "dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts",
        "compile": "tsc -p .",
        "test": "jest --coverage --color",
      }
  • ER
     "scripts": {
        "dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts",
        "compile": "tsc -p .",
        "test": "jest --coverage --color",
        "start": "npm run compile && npm run dev"
      }

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2012-01-31
    • 2017-02-20
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2011-05-10
    相关资源
    最近更新 更多