Search code examples
javascriptnode.jsreactjsparameterspostman

node not showings single details in postman with params


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.


Solution

  • 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