diff --git a/demo/index.html b/demo/index.html index 48095af..612b4f0 100644 --- a/demo/index.html +++ b/demo/index.html @@ -81,6 +81,7 @@ - - + + diff --git a/demo/package.json b/demo/package.json index 5077438..2abf940 100644 --- a/demo/package.json +++ b/demo/package.json @@ -6,11 +6,13 @@ "type": "commonjs", "main": "server.js", "scripts": { - "start": "bun server.js", + "start": "bun run server.js", + "dev": "bun run --watch server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { "@cap.js/server": "^1.0.5", - "express": "^4.21.2" + "@fastify/static": "^8.1.1", + "fastify": "^5.2.1" } } diff --git a/demo/server.js b/demo/server.js index 07fa0fe..4c99670 100644 --- a/demo/server.js +++ b/demo/server.js @@ -1,23 +1,32 @@ -const express = require("express"); -const Cap = require("@cap.js/server"); - -const app = express(); - -app.use(express.json()); - -// app.use("/cap.js", express.static('../widget/src/cap.js')); -// app.use("/cap-floating.js", express.static('../widget/src/cap-floating.js')); +import path from "path"; +import Cap from "@cap.js/server"; +import fastifyStatic from "@fastify/static"; +import Fastify from "fastify"; +const fastify = Fastify(); const cap = new Cap({ tokens_store_path: ".data/tokensList.json", }); -app.get("/", (req, res) => { - res.sendFile("index.html", { root: __dirname }); +fastify.register(fastifyStatic, { + root: path.join(__dirname), + prefix: "/", }); -app.post("/api/challenge", (req, res) => { - res.json( +fastify.get("/", (req, res) => { + res.sendFile("index.html"); +}); + +fastify.get("/cap.js", (req, res) => { + res.sendFile("../widget/src/cap.js"); +}); + +fastify.get("/cap-floating.js", (req, res) => { + res.sendFile("../widget/src/cap-floating.js"); +}); + +fastify.post("/api/challenge", (req, res) => { + res.send( cap.createChallenge({ challengeCount: 32, challengeDifficulty: 3, @@ -25,15 +34,15 @@ app.post("/api/challenge", (req, res) => { ); }); -app.post("/api/redeem", async (req, res) => { +fastify.post("/api/redeem", async (req, res) => { const { token, solutions } = req.body; if (!token || !solutions) { - return res.status(400).json({ success: false }); + return res.code(400).send({ success: false }); } - - res.json(await cap.redeemChallenge({ token, solutions })); + + res.send(await cap.redeemChallenge({ token, solutions })); }); -app.listen(3000, () => { - console.log("Listening on http://localhost:3000"); +fastify.listen({ port: 3000, host: "0.0.0.0" }).then(() => { + console.log("Server is running on http://localhost:3000"); }); diff --git a/server/index.test.js b/server/index.test.js index 25f3ee7..ba28a7d 100644 --- a/server/index.test.js +++ b/server/index.test.js @@ -1,6 +1,5 @@ const express = require('express'); const crypto = require('crypto'); -const fs = require('fs').promises; const Cap = require('./index.js'); const app = express();