-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·131 lines (105 loc) · 3.6 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
121
122
123
124
125
126
127
128
129
130
131
// Import express
"use strict";
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
// Notify using the built-in convenience method
const notifier = updateNotifier({pkg, updateCheckInterval: 1000 * 60 * 60 * 24 * 7});
if (notifier.update) {
console.log(`Update available: ${notifier.update.latest}`);
}
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
const session = require('express-session');
const config = require('./app/config/config');
var validate = require("validate-npm-package-name");
// Import routes
const fn = '/pci/v1';
const userRoutes = require("./app/routes/route-users");
const authRoutes = require("./app/routes/route-auths");
const ordersRoute = require("./app/routes/router.orders");
const apartmentsRoute = require("./app/routes/route.computer");
const app = express();
var cors = require('cors');
var sess = {
secret: 'fdsakhfdsjabgidshngaerniaerpbeijdskagkgsakjnk',
cookie: {
maxAge: 24 * 60 * 60 * 1000,
secure: true,
expires: 0
},
resave: false,
saveUninitialized: true,
};
if (app.get('env') === 'production') {
app.use(function (req, res, next) {
var protocol = req.get('x-forwarded-proto');
protocol === 'https' ? next() : res.redirect('https://' + req.hostname + req.url);
});
app.set('trust proxy', 1);// trust first proxy
sess.cookie.secure = true // serve secure cookies
}
app.use(session(sess));
app.use(cors());
app.use('/uploads', express.static('uploads'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(fn, apartmentsRoute);
app.use(fn, userRoutes);
app.use(fn, authRoutes);
app.use(fn, ordersRoute);
app.use('/api/doc', express.static('docs'));
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
app.get('/', function (req, res) {
res.send(' Server started...');
let sess = req.session;
if (sess.username && sess.id) {
return res.redirect('/');
}
});
/** Connect to Mongoose and set connection variable **/
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
autoIndex: false, // Don't build indexes
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // GiREMOTEve up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
};
mongoose.connect(config.REMOTE_MONGO_URI, options)
.then(() => console.log("Connected to DataBase..."))
.catch(err => console.error("An Error has occured", err));
var db = mongoose.connection;
db.on('open', function () { console.log('OK');});
/** Launch app to listen to specified port **/
const server = app.listen(config.HOSTING_PORT, function () {
console.log("Running RestHub on port " + config.HOSTING_PORT);
});
server.on('listening', function(){
console.log('Ok server is Running ...');
});
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});
}
/** Export server for other external modules **/
module.exports = server;