Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (120 loc) · 3.57 KB
/
Copy pathapp.js
File metadata and controls
139 lines (120 loc) · 3.57 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* JournalAPI
*
* An API for storing journal entries along with
* location data, mood data, and weather data.
*
* CIS 371 - Fall 2021
*
*/
/**********
* Load all the libraries we need.
**********/
var createError = require('http-errors');
var crypto = require('crypto');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var winston = require('winston');
var mongoose = require('mongoose');
var passport = require('passport');
var session = require('express-session');
/**********
* Setup the logger.
* We will store errors in error.log, combined info and errors
* in combined, and if not in production mode we will also
* output to the console.
**********/
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'JournalAPI' },
transports: [
new winston.transports.File({ filename: './logs/error.log', level: 'error' }),
new winston.transports.File({ filename: './logs/combined.log' }),
],
});
if(process.env.NODE_ENV !== 'production'){
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
/**********
* Connect to the mongodb instance.
* If this fails, we should stop the process.
* Notice that using +srv on the protocol causes the
* connection to use ssl by default.
**********/
const uri = 'mongodb+srv://mrwoodring:badpassword@cluster0.lfzod.mongodb.net/Notes?retryWrites=true&w=majority'
mongoose.connect(uri).
catch( error =>
{
logger.error('Cannot connect to mongo instance.');
logger.error(error);
logger.error('Shutting down API.');
process.exit(1);
}
);
mongoose.connection.on('error', err => {
logger.error('Problem with the database connection...');
logger.error(err);
});
/**********
* Load the routes from the route files.
**********/
var indexRouter = require('./routes/index.js');
var usersRouter = require('./routes/users.js');
var entriesRouter = require('./routes/entries.js');
/**********
* Create an express app instance and setup all
* the middleware and configurations.
**********/
var app = express();
var secret = crypto.randomBytes(32).toString('hex');
// We want to save information in a session
// for the web interface.
app.use(session({
secret: secret,
resave: true,
saveUninitialized: true
}));
// view engine setup (for later!)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// Start passport and Ensure that the user information
// is saved to the session.
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Ensure the user object (if there is one logged in)
// is available as a local var so templates can use it.
app.use((req, res, next) => {
res.locals.user = req.user;
next();
});
app.use('/', indexRouter);
app.use('/users', usersRouter.router);
app.use('/entries', entriesRouter.router);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler middleware
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');
});
module.exports = app;