-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (45 loc) · 1.45 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
48
49
50
51
52
const express = require("express");
var cors = require("cors");
const morgan = require("morgan");
const validate = require("valid-url");
const read = require("./utils/read");
const generate = require("./utils/generate");
const update = require("./utils/update");
const check = require("./utils/check");
const app = express();
app.use(cors());
app.use(express.json());
app.use(morgan("tiny"));
app.get("/:uuid", async function (req, res) {
const uuid = req.params.uuid;
const url = await read(uuid);
return res.redirect(url);
});
app.post("/generate", async function (req, res) {
const name = req.body.generationName;
const email = req.body.generationEmail;
const target = req.body.generationTarget;
if (validate.isUri(target)) {
const generated = await generate(name, email, target);
return res
.status(201)
.json({ target: target, url: generated.url, qr: generated.qr });
} else {
return res.status(400).json({ message: "Not a valid URL" });
}
});
app.post("/update", async function (req, res) {
const uuid = req.body.updateUUID;
const target = req.body.updateTarget;
if (await check(uuid)) {
if (validate.isUri(target)) {
const updated = await update(uuid, target);
return res.status(200).json({ message: updated.message });
} else {
return res.status(400).json({ message: "Not a valid URL" });
}
} else {
return res.status(400).json({ message: "Invalid UUID" });
}
});
module.exports = app;