SHARE
TWEET

Untitled

a guest Jun 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3. const port = 3000;
  4. const connection = require('./conf');
  5. const bodyParser = require('body-parser');
  6.  
  7.  
  8. app.use(bodyParser.json());
  9. //To support url-encoded bodies
  10. app.use(bodyParser.urlencoded({
  11.     extended: true
  12.     })
  13. );
  14.  
  15.  
  16. app.get('/', (req, res) => {
  17.     res.send('Hi, this is Express')
  18. })
  19.  
  20. app.get('/api/movies', (req, res) => {
  21.     connection.query('SELECT * from movie', (err, results) => {
  22.         if (err) {
  23.             console.log(err)
  24.             res.status(500).send(err.message);
  25.            
  26.         } else {
  27.             res.json(results);
  28.         }
  29.     });
  30. });
  31.  
  32. app.get('/api/movies/name', (req, res) => {
  33.     connection.query('SELECT name from movie', (err, results) => {
  34.         if (err) {
  35.             res.status(500).send(err.message);
  36.          
  37.         } else {
  38.             res.json(results);
  39.         }
  40.     });
  41. });
  42.  
  43. app.post('/api/movies', (req, res) => {
  44.     // console.log("POST /api/movie", formData);
  45.     const formData = req.body;
  46.        connection.query('Insert into movie set ?', formData, (err, results) => {
  47.            if (err) {
  48.                console.log(err);
  49.                res.results(500).send('There is an error while saving a movie');
  50.            } else {
  51.                res.sendStatus(200);
  52.            }
  53.         });
  54.     });
  55.  
  56. app.put('/api/movies/:id', (req, res) => {
  57.     const idMovie = req.params.id;
  58.     const formData = req.body;
  59.         connection.query('UPDATE movie SET ? WHERE id = ?', [formData, idMovie], err => {
  60.             if (err) {
  61.             console.log(err);
  62.             res.status(500).send("Error editing the movie");
  63.             } else {
  64.             res.sendStatus(200);
  65.             }
  66.         });
  67.     });
  68.  
  69. app.delete('/api/movies/:id', (req, res) => {
  70.     const idMovie = req.params.id;
  71.         connection.query('DELETE FROM movie WHERE id = ?', [idMovie], err => {
  72.             if (err) {
  73.                 console.log(err);
  74.                 res.status(500).send('Error deleting the movie');
  75.             } else {
  76.                 res.sendStatus(200);
  77.             }
  78.         });
  79. });
  80.  
  81. app.listen(port, (err) => {
  82.     if(err) {
  83.         throw new Error('There is an error!')
  84.     }
  85.         console.log(`Im listening on ${port}`)
  86. });
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