[英]Put and delete method is showing 404 in postman
0 I have been banging my head the long hours trying to figure why my PUT and DELETE request does not work. 0 我一直在努力思考为什么我的 PUT 和 DELETE 请求不起作用。 It returns a 404 not found response.
它返回 404 未找到响应。 My POST and GET all work fine.
我的 POST 和 GET 都工作正常。
I use chrome postman我用铬 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);
}
I am trying to create a simple api in Node.js. The http method is not working我正在尝试在 Node.js 中创建一个简单的 api。http 方法不起作用
Add a leading /
to the route definitions:添加前导
/
到路由定义:
app.put('/api/courses/:id', ...);
There is no such thing as relative routes on the server.服务器上没有相对路由之类的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.