Skip to content

Commit cdb3388

Browse files
committed
Move common logic to a method, parse body
1 parent ca619c5 commit cdb3388

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
const express = require("express");
22
const mf2 = require("microformat-node");
33
const undici = require("undici");
4+
const querystring = require("querystring");
45
const pkg = require("./package.json");
56
const app = express();
67
const port = process.env.PORT || 9000;
78

9+
function htmlToMf2(url, html, res) {
10+
mf2.get({ baseUrl: url, html }, (err, data) => {
11+
res
12+
.header("content-type", "application/json; charset=UTF-8")
13+
.send(JSON.stringify(err || data, null, 2));
14+
});
15+
}
16+
817
app.set("view engine", "ejs");
918
app.use(express.static("public"));
1019
app.get("/", async (req, res) => {
1120
if (req.query.url) {
1221
const url = req.query.url;
1322
const { body } = await undici.request(url);
14-
mf2.get({ baseUrl: url, html: await body.text() }, (err, data) => {
15-
res.send(err || data);
16-
});
23+
htmlToMf2(url, body.text(), res);
1724
} else {
1825
res.render("index.html.ejs", {
1926
version: `${pkg.version} (lib: ${mf2.version})`,
2027
});
2128
}
2229
});
2330
app.post("/", (req, res) => {
24-
mf2.get({ baseUrl: req.body.url, html: req.body.html }, (err, data) => {
25-
res.send(err || data);
26-
});
31+
const qsBody = querystring.parse(req.body);
32+
htmlToMf2(qsBody.url, qsBody.html, res);
2733
});
2834

2935
app.listen(port, () => {

0 commit comments

Comments
 (0)