-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.js
More file actions
82 lines (51 loc) · 1.83 KB
/
Copy pathtrack.js
File metadata and controls
82 lines (51 loc) · 1.83 KB
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
81
82
const Koa = require('koa');
var Router = require('koa-router');
const nanoid = require('nanoid/async')
const send = require('koa-send');
require('dotenv').config();
const MongoClient = require('mongodb').MongoClient;
const app = new Koa();
const router = new Router();
const debug = process.env.Debug;
const uri = process.env.mongoURI;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
if(debug)
console.log(err);
const collection = client.db("emailTracker").collection("emails");
router.get('/', async (ctx) => {
ctx.status=404;
ctx.body = {error:"malformed request"}
});
router.get('/idpls', async (ctx) => {
ctx.body = await nanoid.nanoid()
});
router.get("/t/:id", async (ctx) => {
ctx.headers.timestamp = new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"});
await collection.updateOne({id:ctx.params},{$push:{opens:ctx.headers}},{upsert: true }).then(async ()=>{
if(debug)
{
console.log("logged - and about to write ");
console.log(ctx.params);
}
ctx.set('Content-Type', 'image/gif');
ctx.set('Cache-Control','no-store, no-cache, must-revalidate, max-age=0');
ctx.set('Pragma', 'no-cache');
ctx.set('Expires', 0);
await send(ctx,"./signature.gif")
});
});
router.get("/r/:id",async (ctx) => {
if(debug) {
console.log("about to read")
console.log(ctx.params);
}
ctx.body = await collection.findOne({id:ctx.params})
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3050);
console.log("listening on port 3050")
// client.close();
});