-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (75 loc) · 2.12 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
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
passport = require('passport'),
mongoose = require('mongoose'),
config = require('./config/database'),
http = require('http'),
https = require('https'),
fs = require('fs');
const app = express();
// SSL CERTS
const options = {
cert: fs.readFileSync('./sslcert/fullchain.pem'),
key: fs.readFileSync('./sslcert/privkey.pem')
};
// ROUTE FILES
const users = require('./routes/users');
const scrape = require('./routes/scrape');
const recipe = require('./routes/recipes');
// BODY PARSER MIDDLEWARE
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json())
// ACTIVATE CORS
app.use(cors());
// LIMIT MAXIMUM SOCKETS USED TO PREVENT FLOODING
http.globalAgent.maxSockets = 1;
https.globalAgent.maxSockets = 1;
// PASSPORT MIDDLEWARE
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
// CONNECT TO DB
mongoose.connect(config.database);
// ON DB SUCCESS
mongoose.connection.on('connected', () => {
console.log('connected to db: ' + config.database);
});
// ON DB FAILURE
mongoose.connection.on('error', (err) => {
console.log('db error occurred: ', err.message);
});
// STATIC FOLDER
app.use(express.static(path.join(__dirname, 'public')));
const port = 3000;
app.set("view engine", "ejs");
app.use(express.static("public"));
// SET ROUTES
app.use('/users', users);
app.use('', scrape);
app.use('', recipe);
// ERROR MIDDLEWARE
app.use((err, req, res, next) => {
console.log(err);
if (res.headersSent) {
return next(err)
}
// Make this status more appropriate in regards to error
res.status(500);
res.json(err.message)
});
// INDEX ROUTE
app.get('/', (req, res) => {
res.send("working");
});
// WILDCARD ROUTE
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// START SERVER
app.listen(port, () => {
console.log('server started on port: ' + port)
});
// CREATE HTTPS SERVER
https.createServer(options, app).listen(8443);