英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论