-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (46 loc) · 1.57 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
// Speaking Child RESTful API with Token based auth
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
port = process.env.PORT || 8080,
ip = process.env.IP,
methodOverride = require('method-override'),
config = require('./app_api/config/config'),
routes = require('./app_api/routes/routes'),
passport = require('passport');
require('./app_api/config/passport');
// take care of deprecated warning
mongoose.Promise = global.Promise;
mongoose.connect(config().mongodb_uri);
// mongoose.connect('mongodb://localhost/speaking_child');
function requireHTTPS(req, res, next) {
// forces https on web browsers where
// it is not automatic (e.g. chrome)
if (!req.secure && req.get('x-forwarded-proto') !== 'https'
&& process.env.NODE_ENV !== 'development' && req.get('host') !== 'localhost:8080') {
return res.redirect('https://' + req.get('host') + req.url);
}
return next();
}
app.use(requireHTTPS);
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride());
app.use(bodyParser.json());
app.use(express.static(__dirname + '/app_client'));
app.use(passport.initialize());
app.use(routes);
app.use(function(err, req, res, next) {
if (err.name == 'UnauthorizedError') {
res.status(401).json({ 'message': err.name + ':'
+ err.message });
}
});
app.use(function(req, res) {
res.sendFile('index.html', {root: __dirname + '/app_client/'});
});
app.listen(port, ip, function() {
console.log('Server started at port :' + port);
});