Postman api gives error 404
can someone please help , this is my code for
server.js
import express from 'express';
import dotenv from 'dotenv';
import colors from 'colors';
import connectDB from './config/db.js';
import testRoutes from './routes/testRoutes.js';
import cors from 'cors';
import morgan from 'morgan';
//dotenv config
dotenv.config();
connectDB();
//rest object
const app = express();
app.use(express.json());
//routes
app.use("/api1/v1/test", testRoutes);
//port
const PORT = process.env.PORT || 8080;
app.listen(PORT, ()=>{
console.log(
`node server running in ${process.env.DEV_MODE} mode on route ${PORT}`.bgBlue
);
});
and this for
testRoutes.js
import express from "express";
import {testPostController} from "../controllers/testController.js";
const router = express.Router();
//routes
router.post("/test-post", testPostController);
export default router;
and this for
testController.js
export const testPostController = (req, res) => { const { name } = req.body; res.status(200).send(
Your Name Is ${name});};
im trying to build a mern stack job-portal by following this tutorial , https://youtu.be/3th-dRc4ZBE?si=eGnSU9P_1Witch_d . the api part is at video 7 ,
I have tried checking all the code , checked the routes , tried turning off the ssl settings as suggested by some you tube vids , i also checked that the server is up and running all the time , still i get this error ,
Anshika Arora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please check if the following works
Here’s a step-by-step verification based on your code:
Server (server.js
)
import express from 'express';
import dotenv from 'dotenv';
import colors from 'colors';
import connectDB from './config/db.js';
import testRoutes from './routes/testRoutes.js';
import cors from 'cors';
import morgan from 'morgan';
// dotenv config
dotenv.config();
connectDB();
// rest object
const app = express();
// middlewares
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
// routes
app.use("/api1/v1/test", testRoutes);
// port
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`node server running in ${process.env.DEV_MODE} mode on route ${PORT}`.bgBlue);
});
Routes (testRoutes.js
)
import express from "express";
import { testPostController } from "../controllers/testController.js";
const router = express.Router();
// routes
router.post("/test-post", testPostController);
export default router;
Controller (testController.js
)
export const testPostController = (req, res) => {
const { name } = req.body;
res.status(200).send(`Your Name Is ${name}`);
};
Additional Checks:
- Import Paths:
Ensure the import paths are correct and the files are in the expected directories. - Environment Variables:
Check that the.env
file is correctly configured and accessible by thedotenv.config()
call. - Network Issues:
Make sure there are no network issues or port conflicts on your local machine.
By following these steps, you should be able to identify and resolve the 404 error. If the issue persists, please provide any error logs or console messages you are seeing for further assistance.