Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit 9bba62f

Browse files
author
Christopher DeCairos
committed
Update server to be compatible with Express 4
1 parent 7970713 commit 9bba62f

4 files changed

Lines changed: 2824 additions & 1118 deletions

File tree

app/http/routes.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module.exports = function (http, modelsController, webmakerAuth) {
22
var qs = require("querystring"),
3-
express = require("express"),
4-
basicAuth = express.basicAuth,
5-
csrf = express.csrf(),
3+
basicAuth = require('basic-auth-connect'),
4+
csrf = require("csurf")({ cookie: true }),
65
env = require("../../config/environment"),
76
routes = {
87
site: require("./controllers/site"),
@@ -54,10 +53,20 @@ module.exports = function (http, modelsController, webmakerAuth) {
5453
input = req.body;
5554

5655
// Only allow attributes that users should be able to set on their own account
57-
filtered.sendEventCreationEmails = input.sendEventCreationEmails;
58-
filtered.sendMentorRequestEmails = input.sendMentorRequestEmails;
59-
filtered.sendCoorganizerNotificationEmails = input.sendCoorganizerNotificationEmails;
60-
filtered.prefLocale = input.prefLocale;
56+
if (input.hasOwnProperty("sendEventCreationEmails")) {
57+
filtered.sendEventCreationEmails = input.sendEventCreationEmails;
58+
}
59+
if (input.hasOwnProperty("sendMentorRequestEmails")) {
60+
filtered.sendMentorRequestEmails = input.sendMentorRequestEmails;
61+
}
62+
63+
if (input.hasOwnProperty("sendCoorganizerNotificationEmails")) {
64+
filtered.sendCoorganizerNotificationEmails = input.sendCoorganizerNotificationEmails;
65+
}
66+
67+
if (input.hasOwnProperty("prefLocale")) {
68+
filtered.prefLocale = input.prefLocale;
69+
}
6170

6271
req.body = filtered;
6372
next();

app/http/server.js

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = function (env, callback) {
1111
lessMiddleWare = require("less-middleware"),
1212
WebmakerAuth = require("webmaker-auth"),
1313
nunjucks = require("nunjucks"),
14+
morgan = require("morgan"),
15+
errorHandler = require("errorhandler"),
1416
path = require("path"),
1517
route = require("./routes"),
1618
http = express();
@@ -52,80 +54,73 @@ module.exports = function (env, callback) {
5254
});
5355

5456
// Express Configuration
55-
http.configure(function () {
56-
nunjucksEnv.express(http);
57+
nunjucksEnv.express(http);
5758

58-
http.disable("x-powered-by");
59+
http.disable("x-powered-by");
5960

60-
if (!env.get("DISABLE_HTTP_LOGGING")) {
61-
http.use(express.logger());
62-
}
61+
if (!env.get("DISABLE_HTTP_LOGGING")) {
62+
http.use(morgan("combined"));
63+
}
6364

64-
http.use(helmet.iexss());
65-
http.use(helmet.contentTypeOptions());
66-
http.use(helmet.xframe());
65+
http.use(helmet.iexss());
66+
http.use(helmet.contentTypeOptions());
67+
http.use(helmet.xframe());
6768

68-
if (!!env.get("FORCE_SSL")) {
69-
http.use(helmet.hsts());
70-
http.enable("trust proxy");
69+
if (!!env.get("FORCE_SSL")) {
70+
http.use(helmet.hsts());
71+
http.enable("trust proxy");
72+
}
73+
74+
http.use(express.json());
75+
http.use(express.urlencoded({extended: false}));
76+
http.use(webmakerAuth.cookieParser());
77+
http.use(webmakerAuth.cookieSession());
78+
79+
// Setup locales with i18n
80+
http.use(i18n.middleware({
81+
supported_languages: env.get("SUPPORTED_LANGS"),
82+
default_lang: "en-US",
83+
mappings: require("webmaker-locale-mapping"),
84+
translation_directory: path.resolve(__dirname, "../../locale")
85+
}));
86+
87+
// audience and webmakerorg are duplicated because of i18n
88+
http.locals.AUDIENCE = env.get("WEBMAKERORG");
89+
http.locals.WEBMAKERORG = env.get("WEBMAKERORG");
90+
http.locals.profile = env.get("PROFILE");
91+
http.locals.bower_path = "bower_components";
92+
http.locals.personaHostname = env.get("PERSONA_HOSTNAME", "https://login.persona.org");
93+
http.locals.languages = i18n.getSupportLanguages();
94+
95+
var optimize = env.get("NODE_ENV") !== "development",
96+
tmpDir = path.join(require("os").tmpdir(), "mozilla.login.webmaker.org.build");
97+
98+
// convert requests for ltr- or rtl-specific CSS back to the real filename,
99+
// as the rtltr-for-less package was a hack that was never meant to hit production.
100+
http.use(function rtltrRedirect(req, res, next) {
101+
var path = req.path;
102+
if (path.match(/css\/\w+\.(ltr|rtl)\.css/)) {
103+
res.redirect(path.replace(/\.(ltr|rtl)/, ""));
104+
} else {
105+
next();
71106
}
107+
});
72108

73-
http.use(express.json());
74-
http.use(express.urlencoded());
75-
http.use(webmakerAuth.cookieParser());
76-
http.use(webmakerAuth.cookieSession());
77-
78-
// Setup locales with i18n
79-
http.use(i18n.middleware({
80-
supported_languages: env.get("SUPPORTED_LANGS"),
81-
default_lang: "en-US",
82-
mappings: require("webmaker-locale-mapping"),
83-
translation_directory: path.resolve(__dirname, "../../locale")
84-
}));
85-
86-
http.locals({
87-
// audience and webmakerorg are duplicated because of i18n
88-
AUDIENCE: env.get("WEBMAKERORG"),
89-
WEBMAKERORG: env.get("WEBMAKERORG"),
90-
profile: env.get("PROFILE"),
91-
bower_path: "bower_components",
92-
personaHostname: env.get("PERSONA_HOSTNAME", "https://login.persona.org"),
93-
languages: i18n.getSupportLanguages()
94-
});
109+
http.use(lessMiddleWare(path.resolve(__dirname, "public"), {
110+
once: optimize,
111+
debug: !optimize,
112+
dest: tmpDir,
113+
compress: optimize,
114+
yuicompress: optimize,
115+
optimization: optimize ? 0 : 2
116+
}));
95117

96-
// need to make sure router is after i18n.middleware
97-
http.use(http.router);
98-
99-
var optimize = env.get("NODE_ENV") !== "development",
100-
tmpDir = path.join(require("os").tmpdir(), "mozilla.login.webmaker.org.build");
101-
102-
// convert requests for ltr- or rtl-specific CSS back to the real filename,
103-
// as the rtltr-for-less package was a hack that was never meant to hit production.
104-
http.use(function rtltrRedirect(req, res, next) {
105-
var path = req.path;
106-
if (path.match(/css\/\w+\.(ltr|rtl)\.css/)) {
107-
res.redirect(path.replace(/\.(ltr|rtl)/, ""));
108-
} else {
109-
next();
110-
}
111-
});
118+
http.use(express.static(tmpDir));
112119

113-
http.use(lessMiddleWare({
114-
once: optimize,
115-
debug: !optimize,
116-
dest: tmpDir,
117-
src: path.resolve(__dirname, "public"),
118-
compress: optimize,
119-
yuicompress: optimize,
120-
optimization: optimize ? 0 : 2
121-
}));
122-
123-
http.use(express.static(tmpDir));
124-
});
125120

126-
http.configure("development", function () {
127-
http.use(express.errorHandler());
128-
});
121+
if (env.get("NODE_ENV") === "development") {
122+
http.use(errorHandler());
123+
}
129124

130125
route(http, Models, webmakerAuth);
131126

0 commit comments

Comments
 (0)