Express POST不起作用,但GET和PATCH起作用



我正试图为我的项目创建一些RESTAPI,但似乎无法让POST正常工作。我在Postman中测试了GET和PATCH,它们都运行良好。任何帮助都将不胜感激!

产品.服务.ts

export class ProductService {
constructor(private readonly model: Model<ProductType>) {}
public getProduct = async (id: string): Promise<ProductType | null> => {
return await this.model.findOne({ id });
};
public createProduct = async ( 
body: ProductType
): Promise<ProductType> => {
return await this.model.create(body);
};
}
export const productService = new ProductService(ProductModel);

product.controller.ts

export class productController {
constructor(private readonly service: ProductService) {}
public getProduct = async (req: Request, res: Response) => {
const response = await this.service.getProduct(req.params.id);
res.send(response);
};
public createProduct = async (req: Request, res: Response) => {
const response = await this.service.createProduct(req.body);
res.send(response);
};
}
export const productController = new ProductController(
productService
);

产品.路线.ts

const router: Router = express.Router();
router.get('/products/:id', productController.getProduct);
router.post('products/new', productController.createProduct); //this route returns a 404
export default router;

所以当我尝试在Postman上发帖时,它会返回404,但get运行良好。我感谢任何帮助!

正如在对原始帖子的评论中所提到的,我错过了帖子路由URL中的正斜杠。

谢谢!

最新更新