-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (45 loc) · 1.35 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
require('dotenv').config();
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
favicon = require('serve-favicon'),
passport = require('./config/passport'),
routes = require('./config/routes'),
mongoose = require('./config/middleware/mongoose'),
PORT = process.env.PORT || 3000,
app = express();
app.use(favicon(path.join(__dirname, 'static', 'favicon.ico')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/', express.static('static/'));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'app/views'));
passport(app);
app.use(routes);
app.get('*', function (req, res, next) {
let err = new Error(`${req.ip} tried to reach ${req.originalUrl}`); // Tells us which IP tried to reach a particular URL
err.statusCode = 404;
err.shouldRedirect = true; //New property on err so that our middleware will redirect
next(err);
});
app.use((error, req, res, next) => {
console.error(error);
res.render('pages/error', {
user: req.user,
title: error,
error
});
});
// config mongoose
mongoose()
.then(() => {
app.listen(PORT, () => console.log(`Server up and running on ${PORT}.`));
})
.catch(err => {
// an error occurred connecting to mongo!
// log the error and exit
console.error('Unable to connect to mongo.');
console.error(err);
});