Guest User

Untitled

a guest
Nov 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. "use strict";
  2. //import the express app
  3. const express = require('express');
  4. const bodyParser = require('body-parser');
  5.  
  6. //create the express application
  7. const app = express();
  8. app.use(bodyParser.json());
  9. app.use(bodyParser.urlencoded({extended:true}));//localhost:3000/music/?a=1&b=2
  10.  
  11. const music = [
  12. {
  13. id: 1,
  14. title: 'Maps',
  15. Writer: 'Adam Levine Ryan Tedder Benjamin Levin Ammar Malik Noel Zancanella',
  16. Producer: 'Benny Blanco Tedder Zancanella Noah Passovoy',
  17. Length: '3:10'
  18. },
  19. {
  20. id: 2,
  21. title: 'Animals',
  22. Writer: 'Adam Levine Ryan Tedder Benjamin Levin Ammar Malik Noel Zancanella',
  23. Producer: 'Benny Blanco Tedder Zancanella Noah Passovoy',
  24. Length: '3:10'
  25. },
  26. {
  27. id: 3,
  28. title: 'It Was Always You',
  29. Writer: 'Levine Sam Martin Jason Evigan Marcus Lomax Jordan Johnson Stefan Johnson',
  30. Producer: 'Evigan The Monsters and Strangerz S. Johnson',
  31. Length: '4:00'
  32. }
  33. ];
  34.  
  35.  
  36. //set the root route
  37. app.get('/', (req, res) => {
  38. return res.send('Hello World!!');
  39. });
  40.  
  41. app.get('/random', (req, res) => {
  42. // Math.random() * (max - min) + min;
  43. return res.json(Math.random() * (10 - 0) + 0);
  44. });
  45.  
  46. app.get('/music', (req, res) => {
  47. return res.json(music);
  48. });
  49.  
  50. app.post('/music', (req, res) => {
  51.  
  52. //get the parameter from the request body
  53. let id = req.body.id;
  54. let title = req.body.title;
  55. let Writer = req.body.Writer;
  56. let Producer = req.body.Producer;
  57. let Length = req.body.Length;
  58.  
  59. //create new music object
  60. let json_music = {
  61. id,
  62. title,
  63. Writer,
  64. Producer,
  65. Length
  66.  
  67. };
  68. //add the job object to music array
  69. music.push(json_music);
  70.  
  71. //return the music array to server
  72. return res.json(music);
  73. });
  74.  
  75.  
  76. //listen the express app to port 3000
  77. app.listen('3000',() => {
  78. console.log('Application is running on PORT 3000');
  79. });
Add Comment
Please, Sign In to add comment