-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (71 loc) · 2.03 KB
/
app.js
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
const express = require("express");
const bodyParser = require("body-parser");
const utils = require("./utils");
const cors = require("cors");
let app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function middleware(req, res, next){
var string = req.method + " " + req.path + " - " + req.ip;
console.log(string);
next();
});
app.get("/", (req, res) => {
res.send("Welcome to SoundFlow, a music web app player");
});
app.get("/users", (req, res) => {
res.json({
"message": "An API endpoint to create users"
});
});
app.get("/users/all", async (req, res) => {
let result = await utils.getAllUsers();
res.json({
"result": result
});
});
app.post("/users/create", async (req, res) => {
const bodyData = req.body;
let boolIfUserExists = await utils.checkIfUsernameExists(bodyData.username);
if(boolIfUserExists){
utils.createNewUser(bodyData, res);
} else {
res.json({
"message": "Username with the same user already exists"
});
}
});
app.get("/users/delete/all", async (req, res) => {
await utils.deleteRecords();
await utils.deleteAuthTokens();
res.json({
"message": "All records deleted"
});
});
app.post("/users/login", async (req, res) => {
const bodyData = req.body;
utils.loginUser(bodyData.username, bodyData.password, res);
});
app.post("/users/password/update", async (req, res) => {
const bodyData = req.body;
utils.updatePassword(bodyData.username, bodyData.password, bodyData.newPassword, res);
});
app.post("/upload/track", async (req, res) => {
const appSecret = req.headers.app_secret;
if(appSecret.length === 32){
utils.uploadTrack(req, res, appSecret);
} else {
res.status(400).json({
"message": "APP Secret incorrect format"
});
}
});
app.get("/track/:trackID", async (req, res) => {
let trackID = req.params.trackID;
utils.streamSoundTrack(req, res, trackID);
});
app.get("/tracks", async (req, res) => {
utils.fetchAllSoundTracks(req, res);
})
module.exports = app;