forked from codeforgermany/click_that_hood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (42 loc) · 1.38 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
var express = require("express"),
lessMiddleware = require("less-middleware"),
compression = require("compression"),
config = require("config");
function startApp() {
var app = express();
app.use(compression());
app.use(
lessMiddleware(__dirname + "/public", {
compress: process.env.NODE_ENV == "production",
once: process.env.NODE_ENV == "production",
})
);
// Redirect to environment-appropriate domain, if necessary
app.all("*", function (req, res, next) {
if (config.app_host_port != req.headers.host) {
var redirectUrl = "http://" + config.app_host_port + req.url;
console.log("Redirecting to " + redirectUrl + "...");
res.redirect(301, redirectUrl);
} else {
next("route");
}
});
app.get("/:location", function (req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.get("/", function (req, res) {
// redirect old-style /?location=XYZ and /?city=XYZ urls to new-style /XYZ
if (req.query.location) {
res.redirect(301, req.query.location);
} else if (req.query.city) {
res.redirect(301, req.query.city);
} else {
res.sendFile(__dirname + "/public/index.html");
}
});
app.use(express.static(__dirname + "/public"));
app.listen(config.port, null, null, function () {
console.log("The app is running: http://" + config.app_host_port);
});
}
startApp();