-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
40 lines (30 loc) · 908 Bytes
/
index.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
var express = require('express');
var server = require('./server');
var models = require('./models');
var app = express();
// initialize the controllers
app = require('./controllers')(app);
function loadModels(fn) {
models.sequelize.sync({force: true}).complete(fn);
}
function startServer(err) {
if (err) throw err;
var port = process.env.PORT || 3000;
server.use(express.static('./public'));
server.use('/api', app);
server.use(errorHandler);
server.use(notFoundHandler);
if (!module.parent) {
server.listen(port);
console.log('Server has started on port ' + port + '.');
}
}
function errorHandler(err, req, res, next) {
if (~err.message.indexOf('not found')) return next();
console.error(err.stack);
res.status(500).send('An error has occurred.');
}
function notFoundHandler(req, res, next) {
res.status(404).send('Not found');
}
loadModels(startServer);