-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·64 lines (50 loc) · 1.64 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
var express = require('express'),
app = express(),
config = require('./config'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
db = require('./core/db'),
routes = require('./core/routes'),
middleware = require('./core/middleware'),
bodyParser = require('body-parser'),
mkdir = require('./core/utils').mkdir;
/** FOLDER STRUCTURE
* creates the permanent cover/songs directory
*/
console.log("[Server running at " + config.get('env') + "] ("+ process.env.NODE_ENV + ")");
mkdir.mkdirpSync(config.get('temp:dir') + "cover");
mkdir.mkdirpSync(config.get('temp:dir') + "songs");
/** DATABASE
* sets up the database
*/
db.setup();
/** CONFIGURATION
* configure the server
*/
// # serves static files
app.use('/', express.static(__dirname + '/public'));
// # enable morgan dev logger
app.use(morgan('dev'));
// # prepare bodyParser (JSON)
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// # validate id input (needs to be a mongodb-ID to be valid)
app.param('id', middleware.validation.id);
/** ROUTES
* setting up all the routes
*/
app.get('/songs/:id', routes.song.get);
app.get('/resolve', routes.resolve.get);
// # handle request if not successfull yet; must be a 404 Not Found
app.use(function(req, res, next){
res.sendFile( __dirname + '/public/404.html');
});
/** LISTENING
* setting up the server and listening
*/
app.listen(config.get('express:port'), config.get('express:ip'));
console.log("[server started at " + config.get('express:ip') + " on port " + config.get('express:port') + "]");
/** CRON JOBS
* initializing cron jobs
*/
require('./core/cron');