-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.23 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const MongoClient = require("mongodb").MongoClient;
console.log("Starting mini analytics server -- bye bye Google!");
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
var db;
const server = require("http").createServer();
const port = process.env.PORT || 3000;
MongoClient.connect(process.env.MONGO_URL, (err, client) => {
if (err) return console.log(err);
db = client.db("freeanalytics");
app.listen(port, () => {
console.log(`listening on ${port}`);
app.get("/", (_, res) => {
res.send("Server is running!");
});
app.post("/pageload", (req, res) => {
const ip = req.header("x-forwarded-for") || req.connection.remoteAddress;
db.collection("pageload").insertOne(
{
pageload_date: new Date(req.body.pageload_date),
pathname: req.body.pathname,
ip: ip,
},
(err) => {
if (err) return console.log(err);
console.log("Pageload data:", { ...req.body, ip: ip });
console.log("Saved pageload to database!");
res.send("Success!");
}
);
});
});
});