I have simple application where I am using express
server and TypeScript
as a coding language. At first I can hit my endpoint successfully but when I change the model, I see the old data.
I have tried this approach as well but no luck, How to disable webpage caching in ExpressJS + NodeJS?.
What am I missing here?
- My
index.ts
(main) file
/**
* 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
logic
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
logic
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
logic
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;
}
}
- Output (this is my old result)