-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (81 loc) · 2.82 KB
/
server.js
File metadata and controls
95 lines (81 loc) · 2.82 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
require('dotenv').config()
const express = require("express");
const exphbs = require('express-handlebars');
const cookieParser = require("cookie-parser");
const app = express();
var hbs = exphbs.create({ defaultLayout: 'main', extname: 'hbs' });
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
if(process.env.NODE_ENV == "production") {
const compression = require('compression');
const helmet = require('helmet')
app.use(helmet())
app.use(compression());
app.enable('view cache');
}
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(express.static("public"));
app.use(require('./middleware/populateResLocal'));
app.use(require('./middleware/spotifyRenew'));
app.use(require('./middleware/showUsername'));
app.use((req, res, next) => {
res.locals.data.startTime = new Date().getTime();
return next();
});
/* ROUTES */
app.get("/", require('./middleware/showUserPlaylists'), (req, res) => {
res.locals.data.host = process.env.HOSTURL;
res.locals.meta.title = "toolsforspotify";
res.locals.meta.description = "A selection of tools for Spotify Playlists."
if(res.userPlaylists)
res.locals.data.playlists = res.userPlaylists;
res.render("urlbuilder");
});
app.get("/documentation", (req, res) => {
res.locals.data.host = process.env.HOSTURL;
res.locals.meta.title = "Documentation";
res.render("documentation");
});
app.get("/manifest.json", (req, res) => {
res.locals.data.host = process.env.HOSTURL;
res.render("manifest", {layout: false});
});
app.get("/offline", (req, res) => {
res.locals.data.title = "Offline";
res.render("offline");
});
app.use('/authorize', require('./routes/authorize'));
app.use('/callback', require('./routes/callback')); // Spotify API Authorization Callback
app.use('/:p(backup|analyze)', require('./middleware/playlistLoader'));
app.use("/backup", require('./routes/backup'));
app.use("/analyze", require('./routes/analyze'));
app.use("/generate", require('./routes/generate'));
app.use("/token", require('./routes/token'));
app.get("/logout", (req, res) => {
res.clearCookie("access_token");
res.clearCookie("refresh_token");
res.clearCookie("state");
res.clearCookie("user_name");
res.clearCookie("user_id");
res.redirect("/");
});
/* END ROUTES */
/* Error handling */
app.use((req, res, next) => {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = (process.env.NODE_ENV != "production") ? err : {};
res.locals.meta.title = "Error - " + err.status;
res.locals.showLoginButton = err.showLoginButton || false;
res.status(err.status || 500);
res.render('error');
})
/* End error handling */
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});