我有一个简单的应用程序,我使用express服务器和TypeScript作为编码语言。起初,我可以成功地到达端点,但是当我更改模型时,我会看到旧的数据。
我也尝试过这种方法,但是没有运气,How to disable webpage caching in ExpressJS + NodeJS?。
我在这里错过了什么?
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}`);
});route declartions.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)));
}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);
}
};
}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;
}
}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;
}
}

发布于 2021-12-21 16:02:54
在花了将近一天的时间后,我发现我没有在修改后编译我的.ts文件(这从我的角度来说是愚蠢的错误)。因此,很明显,compile不会获得我新添加的更改。
我在package.json中添加了package.json脚本,它将编译我的.ts文件+运行源文件。
这里是package.json文件:
"scripts": {
"dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts",
"compile": "tsc -p .",
"test": "jest --coverage --color",
} "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"
}https://stackoverflow.com/questions/70427852
复制相似问题