-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (96 loc) · 3.27 KB
/
server.js
File metadata and controls
115 lines (96 loc) · 3.27 KB
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
//
// Main Express server for nodejs-demoapp
// ---------------------------------------------
// Ben C, Oct 2017 - Updated: Apr 2019
//
// Dotenv handy for local config & debugging
require('dotenv').config()
// App Insights. Set APPINSIGHTS_INSTRUMENTATIONKEY as App Setting or env var
if(process.env.APPINSIGHTS_INSTRUMENTATIONKEY) {
const appInsights = require("applicationinsights");
appInsights.setup()
.setAutoDependencyCorrelation(true)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(true)
.setAutoCollectExceptions(true)
.setAutoCollectDependencies(true)
.setAutoCollectConsole(true, true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(true);
appInsights.start();
}
// Core Express & logging stuff
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var app = express();
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Logging
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
let mainRoutes = require('./routes/allroutes');
app.use('/', mainRoutes);
let todoRoutes = require('./todo/todo-routes');
app.use('/', todoRoutes);
// Make package app version a global var, shown in _foot.ejs
app.locals.version = require('./package.json').version;
// Catch all route, generate an error & forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
if(req.method != 'GET') {
err = new Error(`Method ${req.method} not allowed`);
err.status = 500;
}
next(err);
});
// Error handler
app.use(function(err, req, res, next) {
console.error(`### ERROR: ${err.message}`);
// App Insights
const appInsights = require("applicationinsights");
if(appInsights.defaultClient) appInsights.defaultClient.trackException({exception: err});
// Render the error page
res.status(err.status || 500);
res.render('error', {
title: 'Error',
message: err.message,
error: err
});
});
// Get values from env vars or defaults where not provided
var port = process.env.PORT || 3000;
var monogUrl = process.env.MONGO_CONNSTR; // Note. NO DEFAULT!
var retries = process.env.MONGO_RETRIES || 5;
var retryDelay = process.env.MONGO_RETRY_DELAY || 30;
// Mongo Connection totally optional
// - really only use it when demo'ing App Insights
if(monogUrl) {
// Include our data-access library for MongoDB
var dataAccess = require('./todo/data-access');
dataAccess.connectMongo(monogUrl, retries, retryDelay)
.then(() => {
// This is important, pass our connected dataAccess - it's a global singleton
app.set('data', dataAccess);
// Normal express connect
var server = app.listen(port, function () {
var port = server.address().port;
console.log(`### Server listening on ${server.address().port}`);
});
})
.catch(err => {
console.error(`### ERROR! Unable to connect to MongoDB!, URL=${process.env.MONGO_CONNSTR}`);
console.error(err.message);
process.exit(-1);
})
} else {
// Start the server, without Mongo
app.listen(port);
console.log(`### Server process listening on port ${port}`);
}
module.exports = app;