Im using a hard coded simple array of objects as data here . can someone tell me why i cant post data from postman in to this.Im able to get the data though ,having trouble only with post . my code is below
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/users',(req,res)=>{
res.send(courses);
});
app.post('/api/courses',(req,res)=>{
const course={
id:courses.length+1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.get('/api/users/:id',(req,res)=>{
const course = courses.find(c=>c.id===Number(req.params.id));
if(!course) res.status(404).send("The course with requested ID is not found");
res.send(course);
});
const port = process.env.PORT || 3000;
app.listen(port,()=>console.log(`listening to port ${port}...`))