I_Jemin

Handling GET Request

const express = require('express');
const app = express();
const courses = [
{ id: 1, name: 'course1'},
{ id: 2, name: 'course2'},
{ id: 3, name: 'course3'},
];
// Corespond to HTTP
// app.get()
// app.post()
......@@ -12,18 +18,17 @@ app.get('/',(req,res)=> {
});
app.get('/api/courses',(req,res) => {
res.send([1,2,3]);
res.send(courses);
});
app.get('/api/courses/:id',(req,res) => {
res.send(req.params.id);
const course = courses.find(c=> c.id === parseInt(req.params.id));
if(!course) res.status(404).send('The course with the given ID was not found');
res.send(course);
});
app.get('/api/courses/:year/:month',(req,res) => {
res.send(req.query);
});
// PORT
const port = process.env.PORT || 3000;
app.listen(port,()=> console.log(`Listening on port ${port}...`));
\ No newline at end of file
app.listen(port,()=> console.log(`Listening on port ${port}...`));
......