daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const Joi = require('joi');
  3. const app = express();
  4.  
  5. const courses = [
  6.     {id: 1, name: `course1`},
  7.     {id: 2, name: `course2`},
  8.     {id: 3, name: `course3`},
  9. ];
  10.  
  11. app.use(express.json());
  12.  
  13. // hello world
  14. app.get(`/`, (req, res) => {
  15.     res.send(`Hello World`);
  16. });
  17.  
  18. // get all courses
  19. app.get(`/api/courses`, (req, res) => {
  20.     res.send(courses);
  21. });
  22.  
  23. // get course by id
  24. app.get(`/api/courses/:id`, (req, res) => {
  25.     const course = courses.find((course) => course.id === parseInt(req.params.id));
  26.     if (!course) {
  27.         res.status(404).send(`The course with ID ${req.params.id} not found!`)
  28.     } else {
  29.         res.send(course);
  30.     }
  31. });
  32.  
  33. // create course
  34. app.post(`/api/courses`, (req, res) => {
  35.     const schema = {
  36.         name: Joi.string().min(3).required()
  37.     };
  38.     const result = Joi.validate(req.body, schema);
  39.     if (result.error) {
  40.         res.status(400).send(result.error.details[0].message);
  41.         return;
  42.     }
  43.  
  44.     const course = {
  45.         id: courses.length + 1,
  46.         name: req.body.name
  47.     };
  48.     courses.push(course);
  49.     res.send(course);
  50. });
  51.  
  52. // update course
  53. app.put(`/api/courses/:id`, (req, res) => {
  54.     const requestId = parseInt(req.params.id);
  55.     const course = courses.find((course) => course.id === requestId);
  56.     if (!course) {
  57.         res.status(404).send(`no course with id: ${requestId}`);
  58.         return;
  59.     }
  60.  
  61.     const schema = {
  62.         name: Joi.string().min(3).required()
  63.     };
  64.     const result = Joi.validate(req.body, schema);
  65.     if (result.error) {
  66.         res.status(400).send(result.error.details[0].message);
  67.         return;
  68.     }
  69.     const arrayId = courses.findIndex((course) => course.id === requestId);
  70.     const newCourse = {
  71.         ...courses[arrayId],
  72.         name: req.body.name
  73.     };
  74.     courses[arrayId] = newCourse;
  75.     res.send(newCourse);
  76. });
  77.  
  78. // delete course
  79. app.delete(`/api/courses/:id`, (req, res) => {
  80.     const requestId = parseInt(req.params.id);
  81.     const course = courses.find((course) => course.id === requestId);
  82.     if (!course) {
  83.         res.status(404).send(`no course with id: ${requestId}`);
  84.         return;
  85.     }
  86.  
  87.     const arrayId = courses.findIndex((course) => course.id === requestId);
  88.     const deletedCourse = courses.splice(arrayId, 1);
  89.     res.send(deletedCourse[0]);
  90. });
  91.  
  92. app.listen(3000, () => console.log(`Listening on port 3000...`));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top