I_Jemin

Handling HTTP Post

1 const express = require('express'); 1 const express = require('express');
2 const app = express(); 2 const app = express();
3 3
4 +app.use(express.json());
5 +
4 const courses = [ 6 const courses = [
5 { id: 1, name: 'course1'}, 7 { id: 1, name: 'course1'},
6 { id: 2, name: 'course2'}, 8 { id: 2, name: 'course2'},
...@@ -27,6 +29,15 @@ app.get('/api/courses/:id',(req,res) => { ...@@ -27,6 +29,15 @@ app.get('/api/courses/:id',(req,res) => {
27 res.send(course); 29 res.send(course);
28 }); 30 });
29 31
32 +app.post('/api/courses',(req,res)=> {
33 + const course = {
34 + id: courses.length + 1,
35 + name: req.body.name
36 + }
37 +
38 + courses.push(course);
39 + res.send(course);
40 +});
30 41
31 // PORT 42 // PORT
32 const port = process.env.PORT || 3000; 43 const port = process.env.PORT || 3000;
......