Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- //import the express app
- const express = require('express');
- const bodyParser = require('body-parser');
- //create the express application
- const app = express();
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({extended:true}));//localhost:3000/music/?a=1&b=2
- const music = [
- {
- id: 1,
- title: 'Maps',
- Writer: 'Adam Levine Ryan Tedder Benjamin Levin Ammar Malik Noel Zancanella',
- Producer: 'Benny Blanco Tedder Zancanella Noah Passovoy',
- Length: '3:10'
- },
- {
- id: 2,
- title: 'Animals',
- Writer: 'Adam Levine Ryan Tedder Benjamin Levin Ammar Malik Noel Zancanella',
- Producer: 'Benny Blanco Tedder Zancanella Noah Passovoy',
- Length: '3:10'
- },
- {
- id: 3,
- title: 'It Was Always You',
- Writer: 'Levine Sam Martin Jason Evigan Marcus Lomax Jordan Johnson Stefan Johnson',
- Producer: 'Evigan The Monsters and Strangerz S. Johnson',
- Length: '4:00'
- }
- ];
- //set the root route
- app.get('/', (req, res) => {
- return res.send('Hello World!!');
- });
- app.get('/random', (req, res) => {
- // Math.random() * (max - min) + min;
- return res.json(Math.random() * (10 - 0) + 0);
- });
- app.get('/music', (req, res) => {
- return res.json(music);
- });
- app.post('/music', (req, res) => {
- //get the parameter from the request body
- let id = req.body.id;
- let title = req.body.title;
- let Writer = req.body.Writer;
- let Producer = req.body.Producer;
- let Length = req.body.Length;
- //create new music object
- let json_music = {
- id,
- title,
- Writer,
- Producer,
- Length
- };
- //add the job object to music array
- music.push(json_music);
- //return the music array to server
- return res.json(music);
- });
- //listen the express app to port 3000
- app.listen('3000',() => {
- console.log('Application is running on PORT 3000');
- });
Add Comment
Please, Sign In to add comment