-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·111 lines (81 loc) · 2.73 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser')
const session = require('express-session');
const {Datastore} = require('@google-cloud/datastore');
const {DatastoreStore} = require('@google-cloud/connect-datastore');
var cors = require('cors')
var formidable = require('express-formidable')
var Initialization = require("./initialization")
const port = process.env.APPLICATION_PORT;
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';
var app = express();
app.use(cors());
//Consider all request as application/json
//app.use(express.json({type: '*/*'}));
// parse application/json
//app.use(bodyParser.json())
/*
app.use(formidable({
uploadDir: '/tmp'
}));
*/
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(session({
store: new DatastoreStore({
dataset: new Datastore(),
kind: 'express-sessions',
}),
secret: 'gcsfileuploader',saveUninitialized: true,resave: false}));
//Dynamic routing based on configuration
const fs = require('fs');
let rawdata = fs.readFileSync('route-config.json');
let routers = JSON.parse(rawdata);
routers.forEach(function (route){
var r = require(route.router);
app.use(route.path, r)
})
// set the view engine to ejs
app.set("view options", {layout: false});
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + "/public/pages");
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// 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');
//res.setHeader('Strict-Transport-Security', 'max-age=10886400; includeSubDomains')
});
app.listen(port)
Initialization.initializeDatabase();
console.log("Authentication server on port : " + port)
module.exports = app;