Express 服务器无法在 Postman 上获取 /products

Posted

技术标签:

【中文标题】Express 服务器无法在 Postman 上获取 /products【英文标题】:Express Server Cannot GET /products on Postman 【发布时间】:2022-01-04 21:35:51 【问题描述】:

我使用 Express、Sequelize 和 mysql 创建了一个服务器。以下代码是我的产品路线:

const express = require('express');
const Product = require('../models/product');

const router = express.Router();

router.get("/", async (req, res) => 
    /* const products = await Product.findAll();
    if (product) 
        res.status(200).send(message: 'Product found successfully', data: products)
     else 
        res.status(404).send(message: 'Product not found')
     */
    res.status(200).end()
);

router.post("/", async (req, res) => 
    try 
        const product = await Product.create(req.body);
        res.send(product)
     catch (error) 
        console.log(error);
    
   
)

module.exports = router;

这是我的 server.js 代码:

const express = require("express");
const app = express();
require("dotenv").config();
const cors = require("cors");
const endpoints = require("express-list-endpoints");
const port = 5555;
const productRouter = require("./src/routes/product");
/* const variantRouter = require("./src/routes/variant"); */

const sequelize = require("./src/db/config");

app.use(express.static(__dirname + "/public"));

var corsOption = 
  origin: true,
  methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
  credentials: true,
  exposedHeaders: ["x-auth-token"],
;

app.use(express.json());
app.use(cors(corsOption))

app.use("products", productRouter);
/* app.use("variant", variantRouter); */

app.get("/", (req, res) => 
  res.send("Server is Healthy!");
);

sequelize.sync( force: true ).then(() => 
  console.log("Drop and re-sync db.");
);
console.log(endpoints(app));

app.listen(process.env.PORT, () => 
  console.log(`server is listening at http://localhost:$process.env.PORT`),
    sequelize.authenticate().then(
      () => console.log("DB Connected"),
      (err) => console.log("Error in connection", err)
    );
);

我这里也有我的模型:

const sequelize = require("../db/config");
const Sequelize = require("sequelize");
const Variant = require("./variants")

const Product = sequelize.define("products", 
  id: 
    type: Sequelize.UUID,
    primaryKey: true,
  ,
  product_name: 
    type: Sequelize.STRING,
    allowNull: false,
  ,
  product_description: 
    type: Sequelize.STRING,
    allowNull: false,
  ,
  date_uploaded: 
      type: Sequelize.DATE,
      allowNull: true
  ,
  date_edited: 
      type: Sequelize.DATE,
      allowNull: true,
  ,
  product_varieties: 
    type : Sequelize.UUID,
    reference: 
        model: 'variant',
        key: 'id'
    
  , 
, 
timestamp: false
);


module.exports = Product; 

我的服务器 JS 中的路由正在显示发送的字符串。 但是来自 product.js 的路线不起作用。告诉我不能 GET 也不能在 /products 上发布

解决办法是什么?

【问题讨论】:

【参考方案1】:

您在app.use("/products") 中忘记了products 之前的/

【讨论】:

谢谢。现在工作【参考方案2】:

您的静态公用文件夹似乎与 API 路由器冲突,请尝试将每个 API 路由器放在 app.use(express.static(__dirname + "/public")); 之前

我在这里找到了解决方案:Express serving static files and routing

【讨论】:

以上是关于Express 服务器无法在 Postman 上获取 /products的主要内容,如果未能解决你的问题,请参考以下文章

我无法将数据存储到 Postman 上的 MongoDB(Atlas)中(Node.js with express)

React 应用程序未从 Express 服务器接收 res.data (JSON),尽管 Postman 确实如此

Express Server 无法从 React Native 应用程序获取请求

REST api/postman - 无法获取/路由错误

Cloudinary\Error:缺少必需的参数 - 文件 - Express 和 Postman

Express + Postman,req.body 为空