Showing
1 changed file
with
11 additions
and
6 deletions
| 1 | const express = require('express'); | 1 | const express = require('express'); |
| 2 | const app = express(); | 2 | const app = express(); |
| 3 | 3 | ||
| 4 | +const courses = [ | ||
| 5 | + { id: 1, name: 'course1'}, | ||
| 6 | + { id: 2, name: 'course2'}, | ||
| 7 | + { id: 3, name: 'course3'}, | ||
| 8 | +]; | ||
| 9 | + | ||
| 4 | // Corespond to HTTP | 10 | // Corespond to HTTP |
| 5 | // app.get() | 11 | // app.get() |
| 6 | // app.post() | 12 | // app.post() |
| ... | @@ -12,18 +18,17 @@ app.get('/',(req,res)=> { | ... | @@ -12,18 +18,17 @@ app.get('/',(req,res)=> { |
| 12 | }); | 18 | }); |
| 13 | 19 | ||
| 14 | app.get('/api/courses',(req,res) => { | 20 | app.get('/api/courses',(req,res) => { |
| 15 | - res.send([1,2,3]); | 21 | + res.send(courses); |
| 16 | }); | 22 | }); |
| 17 | 23 | ||
| 18 | app.get('/api/courses/:id',(req,res) => { | 24 | app.get('/api/courses/:id',(req,res) => { |
| 19 | - res.send(req.params.id); | 25 | + const course = courses.find(c=> c.id === parseInt(req.params.id)); |
| 26 | + if(!course) res.status(404).send('The course with the given ID was not found'); | ||
| 27 | + res.send(course); | ||
| 20 | }); | 28 | }); |
| 21 | 29 | ||
| 22 | -app.get('/api/courses/:year/:month',(req,res) => { | ||
| 23 | - res.send(req.query); | ||
| 24 | -}); | ||
| 25 | 30 | ||
| 26 | // PORT | 31 | // PORT |
| 27 | const port = process.env.PORT || 3000; | 32 | const port = process.env.PORT || 3000; |
| 28 | 33 | ||
| 29 | -app.listen(port,()=> console.log(`Listening on port ${port}...`)); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 34 | +app.listen(port,()=> console.log(`Listening on port ${port}...`)); | ... | ... |
-
Please register or login to post a comment