-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
74 lines (59 loc) · 1.9 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
/*
WIP: when the app is created, we should have options to include:
* sessions
* authentication
This needs to either be:
* options in /bin/miso.bin.js
* options in server.json
* or perhaps a mixture of both?
*/
var express = require('express'),
bodyParser = require('body-parser'),
session = require('express-session'),
// TODO: For dev only
reload = require('./modules/reload'),
path = require('path'),
app = express(),
environment = app.get('env'),
serverConfig = require('./system/config.js')(environment),
routeConfig = require('./cfg/routes.json'),
mvc = require('./system');
// Setup session
// TODO: Set session store
// Fix config: https://github.com/sahat/hackathon-starter/issues/169
serverConfig.session.resave = serverConfig.session.resave || false;
serverConfig.session.saveUninitialized = serverConfig.session.saveUninitialized || false;
app.use(session(serverConfig.session));
// We parse application/x-www-form-urlencoded and application/json
// TODO: Add any further defaults here and make configurable
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Static directory for our client-side JS
app.use(express.static(path.join(__dirname, '/public')));
// Basic error handling
// TODO: make configurable
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Create our miso app
mvc(app, {
routeConfig: routeConfig,
throwUnmappedActions: true,
verbose: true
});
// Run the server
var server = app.listen(serverConfig.port, function () {
var info = server.address(),
address = info.address;
if(address == "::") {
address = "localhost";
}
console.log('');
console.log('Miso is listening at http://%s:%s in %s mode', address, info.port, environment);
console.log('');
// For dev only - auto reloading, TODO: environment support
if(environment !== 'production') {
reload(server, app);
}
});