-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
82 lines (65 loc) · 3.17 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
/* THIS FILE MAKES THE APP RUNNING */
// Module Exports
const express = require('express');
const session = require('express-session');
const morgan = require('morgan');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const bodyParser = require('body-parser');
const path = require('path');
const ejs = require('ejs');
const passport = require('passport');
const flash = require('connect-flash');
/*
const cors = require('cors'); // Cors (Cross-Origin Resource Sharing) allows cross-domain communication from the browser
const jwt = require('jsonwebtoken'); //Json Web Token allows a secure transmission of information between json objects
const passportJWT = require('passport-jwt'); // Passport strategy compatible with Json Web token
*/
// App Starting
const app = express();
const port = process.env.PORT || 8080; // Listen on port defined by the environment, or on the port 8080 by default
app.listen(port, () => {console.log('Smartwork Tracking System listening on port: ' + port)});
// Session & MongoStore
app.use(session({secret: 'anystringoftext', saveUninitialized: true, resave: true, store: new MongoStore(
{
mongooseConnection: mongoose.connection,
ttl: 1 * 01 * 60 * 60 // One hour long session
})}));
// Console logging
app.use(morgan('dev'));
// Connection to MongoDB
mongoose.Promise = global.Promise; // Necessary to avoid an error of connection to the database
var configDB = require('./config/database'); // Retrieve the link to the database in the appropriate config file
mongoose.connect(configDB.database);
mongoose.connection.on('connected', () => {console.log('Connected to database '+configDB.database)}); // when connection is on
mongoose.connection.on('error', (err) => {console.log('Database error: '+err)}); // if error with connection
// View Engine
app.set('views', path.join(__dirname, 'views')); // The views are placed in the views folder
app.set('view engine', 'ejs'); // Specify the engine EJS which combines data and a template to produce HTML code
app.engine('html', require('ejs').renderFile); // Allows to render files with HTML extension
// Static Folder = Folder which includes all the static files (non-generated by the server and unchanged files while requested)
app.use(express.static(path.join(__dirname, 'public'))); // Each Angular or css file will be stored in the "public" folder
// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// Passport & Flash Middlewares
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// Import from files
require('./config/passport')(passport);
// Call to the routes
var home = express.Router();
require('./app/tabs/home_page')(home,passport);
app.use('/', home);
var profile = express.Router();
require('./app/tabs/profile_page')(profile,passport);
app.use('/', profile);
var monitoring = express.Router();
require('./app/tabs/monitoring_page')(monitoring,passport);
app.use('/', monitoring);
/* THE "api.js" FILE NEEDS TO BE CORRECTED
var api = express.Router();
require('./app/API/api')(api,passport);
app.use('/', api);
*/