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

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

node not showings single details in postman with params

问题

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

index.js

import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import { userRouter } from "./routes/users.js";
import { recipesRouter } from "./routes/recipes.js";
const app = express();
app.use(express.json());

app.use(cors());

app.use("/auth", userRouter);
app.use("/recipes", recipesRouter);

mongoose.connect(
    "xxxxxxxxxxxxxxxxxxxxxx"
);
app.listen(3001, () => console.log("服务器已启动"));

router

import express from "express";
import mongoose from "mongoose";
import { RecipeModel } from "../models/Recipes.js";
import { UserModel } from "../models/User.js";
const router = express.Router();
router.get("/createrecipe/:id", async (req, res) => {
    let result = await RecipeModel.findOne({ id: req.params.id });
    if (result) {
        res.send(result);
    } else {
        res.send('没有食谱');
    }
})

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

index.js
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import { userRouter } from "./routes/users.js"
import { recipesRouter } from "./routes/recipes.js"
const app = express();
app.use(express.json());

app.use(cors());

 app.use("/auth", userRouter);
 app.use("/recipes", recipesRouter);

mongoose.connect(
"xxxxxxxxxxxxxxxxxxxxxx"
);
app.listen(3001, () => console.log("SERVER STARTED"));

router

import express from "express";
import mongoose from "mongoose";
import { RecipeModel } from "../models/Recipes.js"
import { UserModel } from "../models/User.js";
const router = express.Router();
router.get("/createrecipe/:id",  async (req, res) =>{
let result =await RecipeModel.findOne({id:req.params.id});
if(result){
    res.send(result)
}else{
    res.send('no recipe')
}})

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

<body>
	<pre>Cannot GET /createrecipe/1</pre>
</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:

确定