-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp-server.js
64 lines (54 loc) · 1.27 KB
/
http-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
55
56
57
58
59
60
61
62
63
64
var app = require("express")();
var pug = require("pug");
var mails = [];
var config;
app.set("views", __dirname + "/views");
app.set("view engine", "pug");
app.get("/", function (req, res) {
res.type("html");
res.render('index', { mails: mails });
});
app.get("/emails", function (req, res) {
res.type("json");
res.send(JSON.stringify(mails, null, 2));
});
// clear mails
app.post("/emails/clear", function (req, res) {
mails.length = 0;
res.redirect("/");
});
app.get("/emails/:index(\\d+)", function (req, res, next) {
var index = +req.params["index"];
if (index < 0 || index >= mails.length) {
return next();
}
if (mails[index].html === undefined || mails[index].html === null) {
// plain text mail
res.type("text");
// disable mime type sniffing
res.set('X-Content-Type-Options', 'nosniff');
} else {
// html mail
res.type("html");
}
res.send(mails[index].html || mails[index].text);
});
app.all("*", function (req, res) {
res.type("text");
res.status(404);
res.send("Not found");
});
function start() {
app.listen(config.httpPort);
}
module.exports = {
start: function () {
start();
},
setMails: function (mailsArg) {
mails = mailsArg;
},
setConfig: function (configArg) {
config = configArg;
}
};