File size: 989 Bytes
01a8e32 c903252 01a8e32 | 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 | import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import { derive } from "./derive-lib.mjs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.disable("x-powered-by");
app.use(express.json({ limit: "1mb" }));
// 静态资源:/static/*
app.use("/static", express.static(path.join(__dirname, "static")));
app.get("/", (_req, res) => {
res.sendFile(path.join(__dirname, "static", "index.html"));
});
app.post("/api/derive", (req, res) => {
try {
const { scheme = "tupa", text = "", options = undefined } = req.body || {};
res.json(derive({ scheme, text, options }));
} catch (e) {
res.status(400).send(e?.message ? String(e.message) : String(e));
}
});
const port = Number.parseInt(process.env.PORT || "8000", 10);
app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`Server listening on http://127.0.0.1:${port}`);
});
|