-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
app.js
112 lines (91 loc) · 2.93 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
103
104
105
106
107
108
109
110
111
112
var dotenv = require("dotenv");
var express = require("express");
var cookieParser = require("cookie-parser");
var session = require("express-session");
var MongoStore = require("connect-mongo")(session);
var mongoose = require("mongoose");
var passport = require("passport");
var flash = require("connect-flash");
var path = require("path");
var http = require("http").createServer(express);
var realFs = require("fs");
var gracefulFs = require("graceful-fs");
const rateLimit = require("express-rate-limit");
var newBaseURL = process.env.NEW_BASE_URL || "http://localhost:8080";
var redirectStatus = parseInt(process.env.REDIRECT_STATUS || 302);
var oldBaseURL = process.env.OLD_BASE_URL;
// Load environment variables file into process.
dotenv.config();
const app = express();
// graceful-fs: in order to delay on EMFILE errors from any fs-using dependencies
gracefulFs.gracefulify(realFs);
// Connect to MongoDB database.
mongoose.connect(process.env.DB_URI || "mongodb://localhost/knoldus");
mongoose.set("useFindAndModify", false);
// Setup passport.
require("./config/passport")(passport);
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
app.set("trust proxy", 1);
const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 500, // limit each IP to 100 requests per windowMs
});
// apply to all requests
app.use(limiter);
// Use cookie parser.
app.use(cookieParser());
app.use(
express.urlencoded({
extended: false,
})
);
app.use(
express.json({
uploadDir: "/images",
})
);
// Set the view engine to ejs.
app.set("view engine", "ejs");
// Setup session storage.
app.use(
session({
store: new MongoStore({ mongooseConnection: mongoose.connection }),
secret: "knoldus",
resave: false,
saveUninitialized: true,
//expires: 1000 * 60 * 60 * 24 * 30, // 1 Month (30 days) see: https://www.npmjs.com/package/connect-mongodb-session
cookie: {
path: "/",
maxAge: 1000 * 60 * 60 * 24 * 30, // 1 Month (30 days) see: https://www.npmjs.com/package/connect-mongodb-session
},
})
);
// Initialize passport.
app.use(passport.initialize());
app.use(passport.session());
// Use the flash.
app.use(flash());
// Static serving of public files.
app.use("/static", express.static(path.join(__dirname, "public")));
app.use(function forceLiveDomain(req, res, next) {
var host = req.get("Host");
if (host === oldBaseURL) {
return res.redirect(redirectStatus, newBaseURL + req.originalUrl);
}
return next();
});
// Get the port we'll listen to.
var port = process.env.PORT || 8080;
/**
* HTTP server for the express application.
*/
const server = app.listen(port, function () {
console.log("Server started.", server.address());
});
server.on("clientError", (err, socket) => {
console.error(err);
socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
});
// Setup routes.
require("./app/routes")(app, passport, server);