|
1 | 1 | const express = require("express"); |
2 | 2 | const mf2 = require("microformat-node"); |
3 | 3 | const undici = require("undici"); |
| 4 | +const querystring = require("querystring"); |
4 | 5 | const pkg = require("./package.json"); |
5 | 6 | const app = express(); |
6 | 7 | const port = process.env.PORT || 9000; |
7 | 8 |
|
| 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 | + |
8 | 17 | app.set("view engine", "ejs"); |
9 | 18 | app.use(express.static("public")); |
10 | 19 | app.get("/", async (req, res) => { |
11 | 20 | if (req.query.url) { |
12 | 21 | const url = req.query.url; |
13 | 22 | 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); |
17 | 24 | } else { |
18 | 25 | res.render("index.html.ejs", { |
19 | 26 | version: `${pkg.version} (lib: ${mf2.version})`, |
20 | 27 | }); |
21 | 28 | } |
22 | 29 | }); |
23 | 30 | 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); |
27 | 33 | }); |
28 | 34 |
|
29 | 35 | app.listen(port, () => { |
|
0 commit comments