Ah, I just solved it. It seems I needed to do this instead:
app.get("/api/data/:serialNumber", async (req, res): Promise<any> => {
Here is the whole file as it stands now:
import express from "express";
import axios from "axios";
import { BEARER_TOKEN } from './config';
import { URL } from './config';
const app = express();
const PORT = 3000;
app.use(express.static("public"));
interface SerialNumberParams {
serialNumber: string;
}
app.get("/api/data/:serialNumber", async (req, res): Promise<any> => {
try {
const serialNumber = req.params.serialNumber;
if (!serialNumber || serialNumber.length < 5) {
return res.status(400).json({ error: "Invalid serial number" });
}
const API_URL = `${URL}${serialNumber}/events?page=1`;
const response = await axios.get(API_URL, {
headers: { Authorization: `Bearer ${BEARER_TOKEN}` }
});
return res.json(response.data);
} catch (error: any) {
return res.status(500).json({ error: error.response?.data || "Error fetching data" });
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});