-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (69 loc) · 2.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var express = require('express');
var bodyParser = require('body-parser');
var utils = require('./app/lib/utils');
var useragent = require('useragent');
function setupMongoose(callback) {
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/plzUpload', function(err) {
if (err) {
console.log("COULD NOT START SERVER : ", err);
process.exit(0);
} else {
mongoose.connection.on('error', function(err, a) {
console.log(err, a);
});
if (callback) callback(mongoose);
}
});
}
function setupRoutes(app, callback) {
// Routes setup
require('./config/routes')(app);
if (callback) callback(app);
}
function setupExpress(callback) {
var app = express();
//===== Configuration =====//
app.set('view engine', 'ejs');
app.set('views', __dirname + '/app/views');
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/public/upload"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.methodOverride());
app.use(express.cookieParser('Node JS is quite cool'));
app.use(express.session({ cookie: { maxAge: 3*60*60*1000 }}));
//app.use(utils.userAuthCheck(app)); // add req.isUserLogged boolean
app.use(utils.moreRender(app));
app.locals.title = 'Plz Upload';
app.locals.projectName = 'PlzUpload';
if ('development' == app.get('env')) {
app.set('port', 3112);
//app.use(express.logger());
} else if ('production' == app.get('env')) {
app.set('port', 80);
}
function isClientSupported(req, res, next) {
useragent(true);
var ua = useragent.is(req.headers['user-agent']);
if (ua.ie || ua.safari || ua.android || ua.mobile_safari) {
res.render('wrongBrowser');
} else {
next();
}
}
app.use(isClientSupported);
if (callback) callback(app);
}
function startServer(app) {
app.listen(app.get('port'), function() {
console.log('Server is running on port ' + app.get('port'));
});
}
setupMongoose(function() {
setupExpress(function(app) {
setupRoutes(app, function(app) {
startServer(app);
});
});
});