节点在Postman中未显示带参数的单个详细信息。

huangapple go评论87阅读模式
英文:

node not showings single details in postman with params

问题

在我的React项目中,我需要使用参数从详情页获取单个详细信息,但响应未发送到Postman。在发送请求后,响应未在Postman中返回。

index.js

  1. import express from "express";
  2. import cors from "cors";
  3. import mongoose from "mongoose";
  4. import { userRouter } from "./routes/users.js";
  5. import { recipesRouter } from "./routes/recipes.js";
  6. const app = express();
  7. app.use(express.json());
  8. app.use(cors());
  9. app.use("/auth", userRouter);
  10. app.use("/recipes", recipesRouter);
  11. mongoose.connect(
  12. "xxxxxxxxxxxxxxxxxxxxxx"
  13. );
  14. app.listen(3001, () => console.log("服务器已启动"));

router

  1. import express from "express";
  2. import mongoose from "mongoose";
  3. import { RecipeModel } from "../models/Recipes.js";
  4. import { UserModel } from "../models/User.js";
  5. const router = express.Router();
  6. router.get("/createrecipe/:id", async (req, res) => {
  7. let result = await RecipeModel.findOne({ id: req.params.id });
  8. if (result) {
  9. res.send(result);
  10. } else {
  11. res.send('没有食谱');
  12. }
  13. })

Postman
使用以下请求URL来获取单个详细信息:

GET请求:http://localhost:3001/createrecipe/1

在详情页和Postman中查看单个详细信息。

英文:

In my react project i need fetch single details to details page with params but the response not sending to postman.after sending request the response not coming in postman

  1. index.js
  2. import express from "express";
  3. import cors from "cors";
  4. import mongoose from "mongoose";
  5. import { userRouter } from "./routes/users.js"
  6. import { recipesRouter } from "./routes/recipes.js"
  7. const app = express();
  8. app.use(express.json());
  9. app.use(cors());
  10. app.use("/auth", userRouter);
  11. app.use("/recipes", recipesRouter);
  12. mongoose.connect(
  13. "xxxxxxxxxxxxxxxxxxxxxx"
  14. );
  15. app.listen(3001, () => console.log("SERVER STARTED"));

router

  1. import express from "express";
  2. import mongoose from "mongoose";
  3. import { RecipeModel } from "../models/Recipes.js"
  4. import { UserModel } from "../models/User.js";
  5. const router = express.Router();
  6. router.get("/createrecipe/:id", async (req, res) =>{
  7. let result =await RecipeModel.findOne({id:req.params.id});
  8. if(result){
  9. res.send(result)
  10. }else{
  11. res.send('no recipe')
  12. }})

postman
get--http://localhost:3001/createrecipe/1

  1. <body>
  2. <pre>Cannot GET /createrecipe/1</pre>
  3. </body>

need to see the single details in details page and postman.

答案1

得分: 0

app.use("/recipes", recipesRouter)中,您正在使用"/recipes"前缀来定义recipesRouter中的所有路由,但您却调用了http://localhost:3001/createrecipe/1而不是http://localhost:3001/recipes/createrecipe/1

英文:

In app.use("/recipes", recipesRouter) You are prefixing all routes in recipesRouter with /recipes but you are calling http://localhost:3001/createrecipe/1 instead of http://localhost:3001/recipes/createrecipe/1

huangapple
  • 本文由 发表于 2023年6月5日 16:13:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定