Cant get POST And DELETE to work with Postman requests (@ Express)

I have nearly completed the first instance of the CRUD-app course, but can’t get past the final stages of validating the POST and DELETE functions.

As far as my humble eyes can see, my code is pretty close to the one Mosh provided for the Vidly app in the next lesson of the course. But I keep getting error messages when sending requests from postman.

Have I done anything wrong? Or is it an update issue with the packages, is there a way around these error?

Here the responses in Postman:

|POST |http://localhost:3000/api/courses/n
// gives 404. Not found. message: Cannot POST /api/courses/n

|DELETE|http://localhost:3000/api/courses/n
// gives 404. Not found. Message Cannot DELETE /api/courses/n

(n as any number)

My code:

const Joi = require(‘joi’);
const express = require(‘express’);
const app = express();

app.use(express.json());


const courses = [
    { id:1, name: 'course1' },
    { id:2, name: 'course2' },
    { id:3, name: 'course3' },
];

app.get('/api/courses', (req, res) => {
    res.send(courses);    
}); 

app.post("/api/courses", (req, res) => {
    const name = { name: req.body.name };
    const schema = Joi.object({ name: Joi.string().min(3).required() });
  
    const result = schema.validate(name);
  
    if (result.error) {
      res.status(400).send(result.error.details[0].message);
    }
 courses.push(course);
    res.send(course);
}); 

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('The course with the given ID was not found.');  
   
    const { error } = validateCourse(req.body);

    if (error) return res.status(400).send(error.details[0].message); // 400 message - 
    
    course.name = req.body.name; // Update course
    res.send(course); // Return the updated course

});

// DELETE Request 
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('The course with the given ID was not found.');  //404

    // Delete
    const index = courses.indexOf(course);
    course.splice(index, 1);
    res.send(course); 

});

//api/courses/1
app.get('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course) return  res.status(404).send('The course with the given ID was not found.');  //404
    res.send(course);

});

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

    return Joi.validate(course, schema); //validate
}

// PORT
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));

How did you end up solving this? or, did you?
just came across the same issue with the DELETE function.
Here’s the response in postman:

<meta charset="utf-8">

<title>Error</title>
<pre>Cannot DELETE /api/courses/2</pre>

It works!
was missing a slash in the url…

Please post your code! It would be great if I could compare it too mine. I am still stuck on this. And as you see: you are the only respondent on the post. I’ve just moved on too another course until I can find a solution.

whoa! That’s a long time to be stuck!
I see you don’t give up, good for you!
I tried to look through your code but couldn’t find anything wrong…
Here’s my code:

const Joi = require('joi');

const express = require(‘express’);
const app = express();

app.use(express.json());

const courses = [
{ id: 1, name: ‘course1’ },
{ id: 2, name: ‘course2’ },
{ id: 3, name: ‘course3’ },
];

app.get(’/’, (req, res) => {
res.send(‘Hello World!!!’);
});

app.get(’/api/courses’, (req, res) => {
res.send(courses);
});

app.get(’/api/courses/:id’, (req, res) => {
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send(‘course with specified ID not found’);
res.send(course);
});

app.post(’/api/courses’, (req, res) => {
const { error } = validateCourse(req.body);
if (error) return res.status(400).send(error.details[0].message);
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
})

const port = process.env.PORT || 3000;

app.listen(port, () => {
console.log(Listening on port ${port}...);
});

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

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(‘course with specified ID 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(‘course with specified ID not found’);
const index = courses.indexOf(course);
courses.splice(index, 1);
res.send(course);
})

Thank you!

I’ll look into it when I have finished the other courses I’m working on right now.

My problems could be an node npm or express issue too, I got several issues installing the backdated versions that are being used in the videos. I guess it will be trial and error until something comes thru.