-
Notifications
You must be signed in to change notification settings - Fork 7
/
webServer.js
80 lines (71 loc) · 2.19 KB
/
webServer.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
// simple web server with JADE and less-middleware support
// will render images and server up static files as well
//
// include modules
var express = require("express"),
app = express(),
path = require("path"),
less = require("less-middleware");
var compression = require("compression");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var port = process.env.PORT || 8080;
app.use(compression());
app
.use(cookieParser()) // i use express.cookieParser, but connect.cookieParser should also work
.use(bodyParser.urlencoded({ extended: false }))
.use(bodyParser.json());
app.set("json spaces", 0);
/** we will need support for web pages to control the mundane functions such
* as password reset. Tell express we are using jade and where
* our views are
*/
app.set("views", __dirname + "/views");
app.set("view engine", "jade");
app.use(function(req, res, next) {
if (req.url.indexOf("Echo") > 0) {
// let heart beat through
next();
return;
}
if ( port != 8080 && !req.secure && req.get("X-Forwarded-Proto") !== "https") {
res.redirect("https://" + req.get("Host") + req.url);
} else next();
});
app.use("/", function(err, req, res, next) {
// handle however you like, special-case based on
// error properties, messages, request conditions etc
// here we'll just render an error page
res.status(err.status || 500);
res.render("error", { error: err });
});
app.get("/web/Echo", function(req, res) {
res.render("echo", { title: "Echo " + new Date().toString(), layout: false });
});
// compile and serve css
app.use(
less(path.join(__dirname, "source", "less"), {
dest: path.join(__dirname, "build"),
options: {
compiler: {
compress: false
}
},
preprocess: {
path: function(pathname, req) {
return pathname.replace("/css/", "/");
}
},
force: true
})
);
// serve static content
app.use(express.static(path.join(__dirname, "build")));
app.get("*", function(req, res) {
res.sendFile(path.resolve(__dirname, "./build/index.html"));
});
// setup server
var server = app.listen(port, function() {
var msg = "Start-up complete, listening on port: " + port;
console.log(msg);
});