Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. const express = require('express');
  2. const connection = require('./config/conf');
  3. const bodyParser = require('body-parser');
  4.  
  5. const app = express();
  6. const port = 3000;
  7.  
  8. // Support JSON-encoded bodies
  9. app.use(bodyParser.json());
  10. // Support URL-encoded bodies
  11. app.use(bodyParser.urlencoded({
  12. extended: true
  13. }));
  14.  
  15. app.get('/', (request, response) => {
  16. response.send('Welcome to Express');
  17. });
  18. // listen to the url "/api/employees" with the verb POST
  19. app.put('/api/employees/:id', (req, res) => {
  20.  
  21. // Get the data sent
  22. const idEmployee = req.params.id;
  23. const formData = req.body;
  24.  
  25. // connection to the database, and insertion of the employee
  26. connection.query('UPDATE employee SET ? WHERE id = ?', [formData, idEmployee], err => {
  27. if (err) {
  28. // If an error has occurred, then the user is informed of the error
  29. console.log(err);
  30. res.status(500).send("Error editing an employee");
  31. } else {
  32. // If everything went well, we send a status "ok".
  33. res.sendStatus(200);
  34. }
  35. });
  36. });
  37.  
  38. // listen to the url "/api/movies" with the verb POST
  39. app.put('/api/movies/:id', (req, res) => {
  40.  
  41. // Get the data sent
  42. const idMovie = req.params.id;
  43. const formData = req.body;
  44.  
  45. // connection to the database, and insertion of the employee
  46. connection.query('UPDATE movie SET ? WHERE id = ?', [formData, idMovie], err => {
  47. if (err) {
  48. // If an error has occurred, then the user is informed of the error
  49. console.log(err);
  50. res.status(500).send("Error editing an employee");
  51. } else {
  52. // If everything went well, we send a status "ok".
  53. res.sendStatus(200);
  54. }
  55. });
  56. });
  57.  
  58. app.listen(port, (err) => {
  59. if (err) {
  60. throw new Error('Something bad happened...');
  61. }
  62.  
  63. console.log(`Server is listening on ${port}`);
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement