'Express returns "not found" intermittently

I've been working with express for years, and I'm a bit rusty on the typescript side - however... I think I have gone blind! Its the first time I encounter this, so I must be doing something wrong...

My problem is this:

  • I have an endpoint called "/status".
  • When I call it, it will sometimes return OK;
  • And other times it will return a 404 Not Found.
  • This is the SAME endpoint doing this intermittently.

Here is my express app's code:

import bodyParer from "body-parser";
import methodOverride from "method-override";
import helmet from "helmet";
import fileUpload from "express-fileupload";
import cors from "cors";
import { errors } from "celebrate";
import { expressRouter } from "./routes";
import config from "../config/config";
import { ErrorHandlers } from "../middleware";

const expressApp = express();

expressApp.use(bodyParer.json());
expressApp.use(methodOverride());
expressApp.use(helmet());
expressApp.use(fileUpload());
expressApp.use(cors());
expressApp.use(errors());

expressApp.use(config.api.v1Prefix, expressRouter);

const errorHandlers = new ErrorHandlers();
expressApp.use(errorHandlers.notFound);
expressApp.use(errorHandlers.handler);

export { expressApp };

This is what the router looks like:

// import user from "../routes/user";

const expressRouter = Router();

expressRouter.get("/status", (req, res) => res.send("OK"));

// user(expressRouter); // TODO: finish this off when above '/status' works...

export { expressRouter };

I also suspect the error handlers we have, and this is what they look like:

import { inversifyContainer } from "../config/inversify_config";
import { TYPES } from "../config/inversify_types";
import { Logger } from "../loaders/logger";
import httpStatus from "http-status";

export class ErrorHandlers {
  loggerDependency = inversifyContainer.get<Logger>(TYPES.LoggerDependency);

  notFound(req, res, next) {
    const error = new Error("Not Found");
    error["status"] = 404;
    next(error);
  }

  public handler(err, req, res, next) {
    const response = {
      code: err.status,
      message: err.message || httpStatus[err.status],
      errors: err.errors,
      stack: err.stack,
    };

    if (process.env.NODE_ENV !== "development") {
      delete response.stack;
    }

    res.status(err.status);
    res.json(response);
  }
}

Has anyone else experienced this before? Please let me know if there is anything else I can provide for assistance.

Up to now I have not been able to identify the issue. Any help will be greatly appreciated!



Solution 1:[1]

So... ehmmm.

We are using cloud foundry (in IBM).

What happened was that, in the past, our continuous deployment got stuck. Then we manually cancelled that deployment, so that the queued-up deployments could run and proceed at the time :)

What I realised today was that the OLD app never got deleted. This was a cloud foundry service that was running on the same domain (url) as the active cloud foundry service, which effectively made it 2 running instances, in other words.

The OLD service obviously did not have this new endpoint and it never would, as it was detached from the deployment.

Once I deleted the old service, this issue completely disappeared.

Thanks for anyone that took the time to read through all this nonense. Cheers!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fred