forked from benc-uk/nodejs-demoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (75 loc) · 2.53 KB
/
server.js
File metadata and controls
94 lines (75 loc) · 2.53 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
//
// Main Express server for nodejs-demoapp
// ---------------------------------------------
// Ben C, Oct 2017 - Updated: Apr 2019
//
console.log(`### Node.js demo app starting...`);
// 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const app = express();
// View engine setup & static content
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
// Logging
app.use(logger('dev'));
// Parsing middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Initialize Passport and AAD authentication
if(process.env.AAD_APP_ID) require('./auth/init')(app)
// Routes
app.use('/', require('./routes.js'));
if(process.env.TODO_MONGO_CONNSTR) app.use('/', require('./todo/routes'));
app.use('/', require('./auth/routes'));
// 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;
// Start the server
app.listen(port);
console.log(`### Server listening on port ${port}`);
module.exports = app;