首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清除inversifyJS中的缓存?

如何清除inversifyJS中的缓存?
EN

Stack Overflow用户
提问于 2021-12-20 20:47:25
回答 1查看 245关注 0票数 0

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

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

我在这里错过了什么?

  1. My index.ts (主)文件

代码语言:javascript
复制
/**
 * 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 declartions.

代码语言:javascript
复制
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逻辑

代码语言:javascript
复制
    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逻辑

代码语言:javascript
复制
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逻辑

代码语言:javascript
复制
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. 输出(这是我的旧结果)

EN

回答 1

Stack Overflow用户

发布于 2021-12-21 16:02:54

在花了将近一天的时间后,我发现我没有在修改后编译我的.ts文件(这从我的角度来说是愚蠢的错误)。因此,很明显,compile不会获得我新添加的更改。

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

这里是package.json文件:

  • AR

代码语言:javascript
复制
    "scripts": {
        "dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts",
        "compile": "tsc -p .",
        "test": "jest --coverage --color",
      }

  • ER

代码语言:javascript
复制
     "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"
      }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70427852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档