SHARE
TWEET

Untitled

a guest Jun 24th, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const connexion = require('./conf');
  3. const bodyParser = require('body-parser');
  4. const app = express();
  5. const port = 3000;
  6.  
  7. app.use(bodyParser.json());
  8. app.use(bodyParser.urlencoded({
  9.   extended: true
  10. }));
  11.  
  12.  
  13. app.get('/api/movies', (req, res) => {
  14.   connexion.query('SELECT * from movie', (err, results) => {
  15.     if (err) {
  16.       res.status(500).send('Erreur lors de la récupération des films');
  17.     } else {
  18.       res.json(results);
  19.     }
  20.   });
  21. });
  22.  
  23.  
  24. app.post('/api/movies', (req, res) => {
  25.   const formData = req.body;
  26.   connexion.query('INSERT INTO movie SET?', formData, (err, results) => {
  27.     if (err) {
  28.       console.log(err);
  29.       res.status(500).send("Erreur lors de la sauvegarde d'un film");
  30.     } else {
  31.       res.sendStatus(200);
  32.     }
  33.   });
  34. });
  35.  
  36. app.put('/api/movies/:id', (req, res) => {
  37.   const idmovie = req.params.id;
  38.   const formData = req.body;
  39.   connection.query('UPDATE movie SET ? WHERE id = ?', [formData, idmovie], err => {
  40.     if (err) {
  41.       console.log(err);
  42.       res.status(500).send("Erreur lors de la modification d'un film");
  43.     } else {
  44.       res.sendStatus(200);
  45.     }
  46.   });
  47. });
  48.  
  49. app.listen(port, (err) => {
  50.   if (err) {
  51.     throw new Error('Something bad happened...');
  52.   }
  53.  
  54.   console.log(`Server is listening on ${port}`);
  55. });
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