-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (83 loc) · 2.4 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
94
95
96
97
98
99
100
101
102
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const passport = require('passport');
const logger = require('morgan');
const fs = require('fs');
const app = express();
require('dotenv').config();
app.set("env", process.env.NODE_ENV || "development");
app.set("host", process.env.HOST || "0.0.0.0");
app.set("port", process.env.PORT || 8080);
// routes
const users = require('./routes/users');
const posts = require('./routes/posts');
const resources = require('./routes/resources');
app.use(logger('common', {
stream: fs.createWriteStream('./access.log', {
flags: 'a'
})
}));
app.use(logger('dev'));
// cors middleware
app.use(cors());
// body parser middleware
app.use(bodyParser());
// passport middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
// static folder
app.use(express.static(path.join(__dirname, 'public')));
// db connection
mongoose.connect(process.env.DATABASE);
mongoose.connection.on('connected', (req, res, next) => {
console.log('Connected to database ' + process.env.DATABASE);
});
mongoose.connection.on('error', (err, next) => {
console.log('Error connecting to database: ' + err);
next(err);
});
// set route prefixes
app.use('/users', users);
app.use('/posts', posts);
app.use('/resources', resources);
// index route
app.get('/', (req, res, next) => {
res.send('Invalid endpoint');
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// dev error handler
// prints stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.code || 500)
.json({
status: 'error',
message: err
});
});
}
// prod error handler
// no stack trace
app.use(function (err, req, res, next) {
res.status(err.status || 500)
.json({
status: 'error',
message: err.message
});
});
// start server
app.listen(app.get("port"), function () {
console.log('\n' + '*******************************');
console.log('REST API listening on port ' + app.get("port"));
console.log('*******************************' + '\n');
});
module.exports = app;