forked from jeghers/playgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (63 loc) · 2.81 KB
/
server.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
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const httpStatus = require('http-status-codes');
const config = require('./app/config');
const { dbInit } = require('./app/db');
const port = process.env.PORT || config.session.port; // set our port
console.log('config...');
console.log(config);
const app = express(); // define our app using express
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
dbInit();
const defaultRouter = express.Router(); // get an instance of the express Router
// middleware to use for all requests
defaultRouter.use((req, res, next) => {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working
// (accessed at GET http://localhost:<port>/api)
defaultRouter.get('/', (req, res) => {
res.json({ status: 'NOOP', message: 'Welcome to the \'playgen\' api.' });
});
// test route to make sure everything is working
// (accessed at GET http://localhost:<port>/api/v1)
defaultRouter.get('/v1', (req, res) => {
res.json({ status: 'NOOP', message: 'V1 is the current version.' });
});
// register our routes -------------------------------
app.use('/api', defaultRouter);
const historyRoute = require('./app/routes/history');
const currentSongRoute = require('./app/routes/currentsong');
const nextSongRoute = require('./app/routes/nextsong');
const songsRoute = require('./app/routes/songs');
const requestsRoute = require('./app/routes/requests');
const playlistsRoute = require('./app/routes/playlists');
// If you need a backend, e.g. an API, add your custom backend-specific middleware here
app.use('/api/v1/playlists/:playlist_id/history', historyRoute);
app.use('/api/v1/playlists/:playlist_id/currentsong', currentSongRoute);
app.use('/api/v1/playlists/:playlist_id/nextsong', nextSongRoute);
app.use('/api/v1/playlists/:playlist_id/songs', songsRoute);
app.use('/api/v1/playlists/:playlist_id/requests', requestsRoute);
app.use('/api/v1/playlists', playlistsRoute);
// catch all the rest as errors
app.use((req, res) => {
// special case of version mismatch
// if (/^\/api\/v[02-9a-zA-Z].*$/.test(req.url)) {
if (/\/api\/v(?!1)/.test(req.url)) {
res.status(httpStatus.NOT_IMPLEMENTED);
res.json({ status: 'ERROR', message: 'Version not supported.' });
return;
}
res.status(httpStatus.NOT_FOUND);
res.json({ status: 'ERROR', message: 'Sorry can\'t find that!' });
});
// start the server
// =============================================================================
app.listen(port);
console.log('Listening on port ' + port);