-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (80 loc) · 2.48 KB
/
app.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
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const favicon = require('serve-favicon');
const path = require('path');
const jwt = require('jsonwebtoken');
const home = require('./routes/home');
const api = require('./routes/api');
const mongoose = require('./config/database'); //database configuration
const appConfig = require('./config/appConfig');
const enforce = require('express-sslify');
const nakedRedirect = require('express-naked-redirect');
const SECRET_KEY = appConfig.secretKey;
const port = appConfig.port;
const app = express();
app.set('secretKey', SECRET_KEY);
app.use(favicon(path.join(__dirname, 'client/public', '/favicon.ico')));
//Add file name as last arg to change initial file to be loaded
//Connect to mongodb
mongoose.connection.on(
'error',
console.error.bind(console, 'MongoDB connection error:')
);
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
if (process.env.NODE_ENV === 'production') {
app.use(nakedRedirect());
app.use(enforce.HTTPS({ trustProtoHeader: true }));
}
//Static files
app.use(express.static(path.join(__dirname, 'client/build')));
app.use(express.static('client/public'));
//Api private route
app.use('/api', validateUser, api);
//Public route
app.use('/', home);
app.get('*', (req, res) =>
res.sendFile(path.join(__dirname, '/client/build', 'index.html'))
);
//handle 404 error
app.use((req, res, next) => {
let err = new Error('Not found');
err.status = 404;
next(err);
});
function validateUser(req, res, next) {
jwt.verify(
req.headers['x-access-token'],
req.app.get('secretKey'),
(err, decoded) => {
if (err) {
res.json({
status: 'error',
message: err.message,
data: null,
});
} else {
//add user id to request
req.body.userId = decoded.id;
next();
}
}
);
}
//handle errors
app.use((err, req, res, next) => {
console.log(err);
if (err.status === 404) {
res.status(404).json({ message: 'Not found' });
} else if (err.name === 'MongoError' && err.code === 11000) {
//pass
} else {
res.status(500).json({ message: 'Something broke!' });
}
});
app.listen(port, () => {
console.log('Node server listening on port ' + port);
});
module.exports = { validateUser };