NodeJS + Express:服务器无法解析GET, PUT, POST或DELETE请求(状态404)



server.js

const express = require('express')
const dotenv = require('dotenv').config()
const port = process.env.PORT ||9999
const goals = require('./routes/goalRoutes')
const app = express()
app.use('/api/goals', goals)
app.listen(port, () => console.log(`Server started on port ${port}`))

goalRoutes.js定义路由api/goals

const express = require('express')
const router = express.Router()
const { createGoal, getGoals, updateGoal, deleteGoal, } = require('../controllers/goalController')
router.route('/').post(createGoal).get(getGoals)
router.route('/:id').put(updateGoal).delete(deleteGoal)
module.exports = router

goalController.js为该路由提供功能

//@desc     Create goal
//@route    POST /api/goals
//@access Private
const createGoal = (err, req, res, next) => {
res.status(200).json({message: "Create goal"})
}
//@desc     Get goals
//@route    GET /api/goals
//@access Private
const getGoals = (err, req, res, next) => {
res.status(200).json({message: "Read goals"})
}
//@desc     Update goal
//@route    PUT /api/goals:id
//@access Private
const updateGoal = (err, req, res, next) => {
res.status(200).json({message: `Update this goal: ${req.params.id}`})
}
//@desc     Get goals
//@route    DELETE /api/goal/:id
//@access Private
const deleteGoal = (err, req, res, next) => {
res.status(200).json({message: `Delete this goal: ${req.params.id}`})
}
module.exports = {
createGoal,
getGoals,
updateGoal,
deleteGoal,
}

我已经创建了一条路由(预期为/api/goals)和该路由的控制器。然后,我使用goalController.js中的控制器来处理GET、PUT、POST和DELETE请求,并将其连接到goalRoutes.js

使用Postman进行测试,我只能解析这些带有404状态的请求。

我最初的想法是我没有正确地将控制器函数导入goalRoutes.js,但我似乎已经将它们导出为一个对象,并且Node在运行时似乎没有任何问题。

我曾经把控制器的函数体放在router.getrouter.postrouter.putrouter.delete的回调函数中,但那真的很乱。我强烈希望不要再回到那个混乱的局面。

我很茫然。为什么要停止router.route的回调函数来使用控制器函数?为什么邮差收到404?

如果我的术语很糟糕,我道歉。我完全是Javascript的初学者。

您已将所有控制器注册为错误处理中间件。

以与其他中间件函数相同的方式定义错误处理中间件函数,除了错误处理函数有四个参数而不是三:(err, req, res, next)

删除第一个err参数

const createGoal = (req, res, next) => {
res.json({message: "Create goal"})
}
const getGoals = (req, res, next) => {
res.json({message: "Read goals"})
}
const updateGoal = (req, res, next) => {
res.json({message: `Update this goal: ${req.params.id}`})
}
const deleteGoal = (req, res, next) => {
res.json({message: `Delete this goal: ${req.params.id}`})
}

仅供参考,使用res.json()时默认的响应状态是200。

如果你使用nodejs模块导入尝试在你的代码中实现它,它可以工作

export default ()=> {
const app = Router()
RolesRoute(app)
auth(app)
FlightRoute(app)
MaintenanceRoute(app)
Books(app)
app.use('/user',UserRoute())
app.use('/engineer', EngineerRoute())
app.use('/instructor', InstructorRoute())
return app
}

认证路线


import { Router } from 'express'
import {AuthValidator} from "../../../lib/validator";
import AuthController from "../../../controllers/auth.controller";
import {isAdmin, isAuth} from "../../../lib/middleware/auth";
const route = Router();
export default (app)=> {
app.use('/auth', route);
/**
* Sign in
*/
route.post("/signIn", AuthValidator.signInValidator, AuthController.signIn)
}

最新更新