0

My api recives every 45 minutes a request:

GET http://MyHost/mediciones/sigfox_libelium/{device}/{data}/{time}/{customData#trama}

I want my code to save {device}, {data}, {time} and {customData#trama} into different variables so I can parse it into readable values(all data it is send hexadecimal) and insert them into my database. How do I take those values out of the URL?

Also, what is the purpose of req, res? I guess It stands for request, respond.Respond sends back to the client but, request? I dont really understand how it works. Im learning all this new, hope someone can help.

This is my API code, I tried it with postman and it works fine so far:

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);
        }
    );
});

1
  • in Express you can use Route Parameters to get data from a URL. (scroll down a bit on the page) Commented Jun 28, 2021 at 10:47

1 Answer 1

1

You need to write the path in this format, then extract your params from req.params.

app.post("mediciones/sigfox_libelium/:device/:data/:time/:customData", (req, res) => {
   const {device, data, time, customData} = req.params
}

I'm not sure what #trama is meant to be, but I guess you can't use # in the route pattern.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.