-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (74 loc) · 2.35 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var express = require('express');
var expressLayouts = require('express-ejs-layouts');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var http = require('http');
var config = require('./config');
var moment = require('moment');
var shortDateFormat = "dddd MMM Do YYYY, @ h:mm:ss A"; // this is just an example of storing a date format once so you can change it in one place and have it propagate
app.locals.moment = moment; // this makes moment available as a variable in every EJS page
app.locals.shortDateFormat = shortDateFormat;
//
////ADD CONTROLLERS HERE
//
var searchController = require('./routes/searchController');
var homeController = require('./routes/homeController');
var diveController = require('./routes/diveController');
var tweetController = require('./routes/tweetController');
app.use(session({
resave: true,
saveUninitialized: true,
secret: 'user'
}));
//
////ERROR HANDELING
//
function logErrors(err, req, res, next) {
console.error(err.stack)
next(err)
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.status(500).send({ error: 'Something failed!' })
} else {
next(err)
}
}
function errorHandler(err, req, res, next) {
res.status(500)
res.render('error', { error: err })
}
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}));
// parse application/json
app.use(bodyParser.json());
app.use(expressLayouts);
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
// app.use(express.errorHandler(serverConfig.errorHandlerOptions));
// set view engine
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
//
////SET IN APP VARIABLES
//
app.use('/css', express.static(__dirname + '/public/css/'));
app.use('/fonts', express.static(__dirname + '/public/fonts/'));
app.use('/scripts', express.static(__dirname + '/public/scripts/'));
//
////DEPEND ON CONTROLLERS HERE
//
app.use('/', searchController);
app.use('/', homeController);
app.use('/', diveController);
app.use('/', tweetController);
//
////CREATE APP > LIST ON SERVER_PORT > CONSOLE LOG INFO
//
http.createServer(app).listen(config.server().SERVER_PORT, function() {
console.log('Go To >> http://localhost:3000/')
});