-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (46 loc) · 1.08 KB
/
server.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
var express = require("express");
var session = require("express-session");
var app = express();
app.use(express.urlencoded({ extended: true }));
app.use(
session({
saveUninitialized: false,
secret: "some global secret string",
resave: true
})
);
app.use("/static", express.static("static"));
app.all("/*", function(req, res) {
var path = req.path;
if (path == "/") {
path = "/index.js";
}
var globals = getGlobals(req, res);
try {
globals.include(path);
res.end();
} catch (e) {
if (e.toString().indexOf("Cannot find module") >= 0) {
res.sendStatus(404);
} else {
throw e;
}
}
});
function getGlobals(req, res) {
var globals = {
include: function(path) {
if (path.charAt(0) != "/") {
path = "/" + path;
}
var module = require("." + path); // leading dot for local path resolution
return module(globals);
},
redirect: res.redirect.bind(res),
request: req.body,
session: req.session,
write: res.write.bind(res)
};
return globals;
}
app.listen(process.env.PORT || 5000);