-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
106 lines (85 loc) · 1.9 KB
/
app.js
File metadata and controls
106 lines (85 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const app = express();
var axios = require("axios");
const songs = [
{
id: 1,
title: 'Never Gonna Give You Up',
artist: 'Rick Astley',
},
{
id: 2,
title: 'First Times',
artist: 'Ed Sheeran',
},
{
id: 3,
title: 'Into The Unknown',
artist: 'Unknown',
},
{
id: 4,
title: 'Perfect',
artist: 'Ed Sheeran',
},
{
id: 5,
title: 'Pay Phone',
artist: 'Maroon 5',
},
]
//CONTROLLERS
const {getTitles} = require('./controllers/titles')
const {getArtists} = require('./controllers/artists')
//MIDDLEWARES
app.use((req,res,next) => {
console.log(new Date().toJSON())
next()
})
app.use((req,res,next)=>{
req.songs=songs
next()
})
app.use(express.json())
//ROUTES
app.get('/',(req,res)=>{
res.json(songs)
})
app.get('/titles',getTitles)
app.get('/artists',getArtists)
app.get('/songs', (req,res)=>{
const array = songs.filter((s)=>{return s.artist==req.query.artist})
res.json(array)
})
app.get('/songs/:id',(req,res)=>{
const temp = songs.filter((s)=>{return s.id==req.params.id})
res.json(temp)
})
app.post('/song',(req,res)=>{
songs.push(req.body)
res.json(songs)
res.sendStatus(200)
})
app.patch('/song/:id',(req,res)=>{
for (element of songs){
if(element.id==req.params.id){
element.artist=req.body.artist
break;
}
}
res.json(songs)
res.sendStatus(200)
})
app.delete('/song/:id',(req,res)=>{
let index = songs.findIndex((s)=> s.id==req.params.id)
songs.splice(index,1)
res.status(200).json(songs)
})
app.get('/data' , async (req,res)=>{
const da = axios.get("https//jsonplaceholder.typicode.com/todos/1")
console.log(da)
})
//STARTING THE SERVER
app.listen(3000,()=>{
console.log('Listening on port 3000')
})