javascriptnode.jstypescriptexpress

Error while building node typescript app specifically in routes file


I have created a routes file which has following code,

import express, { NextFunction, Request, Response } from "express";

const router = express.Router();

router.post(
  "/product",
  async (req: Request, res: Response, next: NextFunction) => {
    return res.status(200).json({});
  }
);

export default router;

See pretty simple just one route in there. But am having a red line under the 7th line of above code, let me show you how,

enter image description here

Running it locally works fine but when I try to build the code and try running the build code it throws an error,

enter image description here

Can anyone help if faced something similar before or know why is this happening? I have tried putting return type like Promise but that do not fixes the error I was thinking this might be an issue because of that.


Solution

  • A middleware or router handler isn't expected to return anything except a promise, which is supported only in Express 5.x.

    It should be:

      async (req: Request, res: Response, next: NextFunction) => {
        res.status(200).json({});
      }