-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
120 lines (98 loc) · 3.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require('babel/register');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var flash = require('express-flash');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var exphbs = require('express-handlebars');
var mongoose = require('mongoose');
var passport = require('passport');
let morgan = require('morgan');
// Load environment variables from .env file
dotenv.load();
const Config = require('./config/Config');
// Controllers
var HomeController = require('./controllers/home');
var userController = require('./controllers/user');
var contactController = require('./controllers/contact');
// Passport OAuth strategies
require('./config/passport');
var app = express();
mongoose.connect(Config.mongoLocal);
mongoose.connection.on('error', ()=>{
throw ('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
mongoose.connection.on('open', ()=>{
console.log('MongoDB Connection Success');
});
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function (a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON: function (object) {
return JSON.stringify(object);
}
}
});
/**
* FOR LOGS
*/
app.use(morgan('dev'));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('port', Config.port);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(expressValidator());
app.use(methodOverride('_method'));
app.use(session({secret: Config.secret, resave: true, saveUninitialized: true}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
res.locals.user = req.user;
next();
});
app.use(express.static(path.join(__dirname, 'public')));
/**
* ALLOW CROSS ORIGIN
*/
app.all('/', (req,res,next)=>{
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.get('/', HomeController.index);
app.get('/get-data',HomeController.get_data);
app.get('/get-id',HomeController.get_id);
app.get('/api-get-data',HomeController.api_get_data);
app.get('/get-all',HomeController.get_all);
app.get('/api-get-metadata',HomeController.api_get_metadata);
app.post('/post-data',HomeController.post_data);
app.post('/update-data',HomeController.update_data);
app.post('/delete-data',HomeController.delete_data);
// Production error handler
if (app.get('env') === 'production') {
app.use(function (err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), ()=> {
console.log('Express server listening on port ' + app.get('port'));
});
//TODO gerer fermeture de connection à la base de données
module.exports = app;