Postman api 给出错误 404 有人可以帮忙吗,这是我的代码 服务器.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
);
});
这对于 测试路由.js
import express from "express";
import {testPostController} from "../controllers/testController.js";
const router = express.Router();
//routes
router.post("/test-post", testPostController);
export default router;
这对于 测试控制器.js
export const testPostController = (req, res) => { const { name } = req.body; res.status(200).send(
你的名字是${name});};
我正在尝试按照本教程构建一个 Mern Stack 工作门户,https://youtu.be/3th-dRc4ZBE?si=eGnSU9P_1Witch_d。 api部分在视频7,
我已经尝试检查所有代码,检查路由,尝试按照一些youtube视频的建议关闭ssl设置,我还检查服务器是否一直启动并运行,但我仍然收到此错误,
请检查以下是否有效
这是根据您的代码进行的逐步验证:
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);
});
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;
testController.js
)export const testPostController = (req, res) => {
const { name } = req.body;
res.status(200).send(`Your Name Is ${name}`);
};
.env
文件是否已正确配置并可通过 dotenv.config()
调用进行访问。通过执行以下步骤,您应该能够识别并解决 404 错误。如果问题仍然存在,请提供您看到的任何错误日志或控制台消息以获得进一步帮助。