簡體   English   中英

放入和刪除方法在postman中顯示404

[英]Put and delete method is showing 404 in postman

0 我一直在努力思考為什么我的 PUT 和 DELETE 請求不起作用。 它返回 404 未找到響應。 我的 POST 和 GET 都工作正常。

我用鉻 postman

app.put('api/courses/:id', (req, res) => {

    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course) return res.status(404).send('This course with the given id was not found');

    const { error } = validateCourse(req.body);
    if (error) 
        return res.status(400).send(error.details[0].message);

    course.name = req.body.name;
    res.send(course);

});

app.delete('api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course) return res.status(404).send('this course with the given ID is not valid');
  
    const index = courses.indexOf(course);
    courses.splice(index, 1)

    res.send(course);

})

function validateCourse(course) {
    const schema = {
        name: Joi.string().min(3).required()
    };

    return Joi.validate(course, schema);

}

我正在嘗試在 Node.js 中創建一個簡單的 api。http 方法不起作用

添加前導/到路由定義:

app.put('/api/courses/:id', ...);

服務器上沒有相對路由之類的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM