Public
Express
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
import { randomGenerator } from "https://esm.town/v/stevekrouse/randomGenerator";
import { pixelData } from "https://esm.town/v/stevekrouse/pixelData";
export async function pixel(req: express.Request, res: express.Response) {
if (req.path === "/") {
let html = `
<div style="display:flex; flex-direction:column; gap: 20px">
<h1>Tracking events</h1>
<img height="100" src="/track">
<input></input>
${
pixelData.reverse().map((d) => `
<div>
<div>${d.ip}</div>
<div>${d.userAgent}</div>
<div>${d.path}</div>
<div>${JSON.stringify(d.query)}</div>
</div>
`).join("\n")
}
</div>
<script>
let img = document.getElementsByTagName("img")[0]
let input = document.getElementsByTagName("input")[0]
input.addEventListener('input', function(e){
img.src="/track?id=" + e.target.value
})
</script>
`;
res.send(html);
}
else if (req.path.startsWith("/track")) {
// TODO lookup ip location http://ip-api.com/json/172.85.34.113
let data = {
query: req.query,
path: req.path,
ip: req.get("true-client-ip"),
userAgent: req.get("user-agent"),
};
pixelData.push(data);
res.set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
await randomGenerator(req, res); // res.set("Content-Type", "image/svg+xml");
// res.redirect("http://delian.io/transparent_pixel.png");
// return data;
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
stevekrouse-pixel.express.val.run
October 23, 2023