2

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.

2
  • Could you share us the url you used to test the fetchTweet route just to make sure there is no mistake there? Commented Dec 16, 2021 at 8:37
  • Yeah sure. 127.0.0.1:3000/api/tweets?id=61b9af93e88ee4de51c28b95 Commented Dec 16, 2021 at 8:59

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.