-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (79 loc) · 3.36 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
90
91
'use strict';
const createError = require('http-errors');
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
// const cookieParser = require('cookie-parser');
const session = require('express-session');
const logger = require('morgan');
const fs = require('fs');
// load routers
const driver_router = require('./routes/driver_router');
const parking_station_router = require('./routes/parking_station_router');
const admin_router = require('./routes/admin_router');
const app = express();
// view engine setup
app.engine('hbs', exphbs.engine({ defaultLayout: 'main', extname: '.hbs' }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// Database
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database(":memory:", (err) => {
if (err) { console.log("Error Occurred - " + err.message); }
else { console.log("DataBase Connected"); }
})
// log
// morgan.token('sessionid', function(req, res, param) { return req.sessionID;});
// morgan.token('user', function(req, res, param) { return req.session.user;});
const log_stream = fs.createWriteStream(path.join(__dirname, 'logfile.log'), { flags: 'a' })
// app.use(logger(':date[iso] // :remote-addr // :remote-user // :url // :referrer // :sessionid // :user // :http-version // :status // :method // :response-time ms //', {stream: log_stream}));
app.use(logger(':date[iso] // :remote-addr // :remote-user // :url // :referrer // :http-version // :status // :method // :response-time ms //', { stream: log_stream }));
// body parser
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// session
// app.use(cookieParser());
app.use(session({
name: 'Parkaro_app_session',
secret: process.env.secret || "PynOjAuHetAuWawtinAytVunarAcjeBlybEshkEjVudyelwa",
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 60000 * 60, //1 ώρα
httpOnly: true,
sameSite: true,
// secure: true //Το cookie θα σταλεί μόνο μέσω https. Σε απλό http δε θα λειτουργήσει
},
// store: new MemoryStore({ checkPeriod: 86400000 })
}))
// app.use((req, res, next) => { res.locals.userId = req.session.loggedUserId; next(); })
app.use(express.static(path.join(__dirname, 'public')));
// use routers
app.use('/', driver_router);
app.use('/parking_station', parking_station_router);
app.use('/admin', admin_router);
// bootstrap
app.use('/css', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/css')))
app.use('/js', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/js')))
// app.use('/js', express.static(path.join(__dirname, 'node_modules/jquery/dist')))
// user photos
app.use('/user_images', express.static('user_images'));
// font-awesome
app.use('/fa', express.static(__dirname + '/node_modules/font-awesome/css'));
app.use('/fonts', express.static(__dirname + '/node_modules/font-awesome/fonts'));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error', {
'lang': req.session.lang
});
});
module.exports = app;