0 I have been banging my head the long hours trying to figure why my PUT and DELETE request does not work. It returns a 404 not found response. My POST and GET all work fine.
I use chrome 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
Add a leading /
to the route definitions:
app.put('/api/courses/:id', ...);
There is no such thing as relative routes on the server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.