Every 45 minutes, my API receives a request:
GET http://MyHost/mediciones/sigfox_libelium/{device}/{data}/{time}/{customData#trama}
I need to extract {device}, {data}, {time}, and {customData#trama} from the URL and store them in separate variables. This data is in hexadecimal format and I want to parse it into readable values, then insert it into my database. How can I retrieve these values from the URL?
Additionally, I have a question about req, res. I understand that res is for responding to the client, but I'm not sure how req works. I'm new to this and would appreciate some guidance.
Below is the code for my API. I've tested it with Postman and it's working correctly:
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const Pool = require("pg").Pool;
const pool = new Pool({
user: "mgr@stanpgtest",
host: "stanpgtest.postgres.database.azure.com",
database: "my db name",
password: "my pass",
port: 5432
});
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.listen(8000, () => {
console.log(`Server is running, listening to port 8000`);
});
app.post("mediciones/sigfox_libelium/{device}/{data}/{time}/{customData#trama}", (req, res) => {
const { label, status, priority } = req.body;
pool.query(
"select now()",
(error, results) => {
if (error) {
throw error;
}
res.send(results);
}
);
});