I know there are several version of this but I can't seem to find a solution for mine. My server doesn't seem to see all routes in my routes file. It only matches the first one.
index.js
import express from 'express'
import mongoose from 'mongoose'
import dotenv from "dotenv";
import tweetRoutes from "./src/routes/tweetRoutes.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
app.use(express.json());
// Routes
app.use("/api/tweets", tweetRoutes);
// DB connection
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.on("connected", () =>
console.log("DB Connection Established!")
);
// Server
app.listen(PORT, () => console.log(`Server listening on: ${PORT}`))
tweetRoutes.js
import { Router } from 'express'
import {
createTweet,
fetchTweets,
fetchTweet,
updateTweet,
deleteTweet,
} from "../controller/tweetController.js";
const router = Router();
router.get("/", fetchTweets);
router.get("/:id", fetchTweet);
router.post("/create-tweet", createTweet);
router.patch("/:id", updateTweet); // Not working
router.delete("/:id", deleteTweet); // Not working
export default router;
In this case, the only route accessible is the first one, fetchTweets. Even when I pass an ID param to get a single tweet using the fetchTweet route, it routes to the first route. The other routes return a 404.
What could I be doing wrong? Any help is highly appreciated.
CodePudding user response:
Thank you for providing the url you used to test the fetchTweet route.
You should use the following path instead to reach the desired route:
127.0.0.1:3000/api/tweets/61b9af93e88ee4de51c28b95
Reference: