I would like to pass the lat lon from this URL http://localhost:8080/fp?lon=103.742463567216646&lat=1.336711421273283. Where should I place the "req.query.lon" and "req.query.lat" in the following code?
I am hoping to parse the users input in the URL link so that I can dynamically retrieve from the database.
const {Client} = require("pg")
const express = require ("express")
const url=require('url')
const fs= require('')
const app = express();
app.use(express.json())
const client = new Client({
"user": "xxx",
"password" : "xxx",
"host" : "xxx",
"port" : xxx,
"database" : "xxx"
})
//app.get("/", (req, res) => res.sendFile(`${__dirname}/index.html`))
app.get("/fp", async (req, res) => {
//const lon = 103.742463567216646;
//const lat = 1.336711421273283;
//const lon = req.query.lon;
//const lat = req.query.lat;
const rows = await readTodos ();
res.send(JSON.stringify(rows))
})
app.listen(8080, () => console.log("Web server is listening.. on port 8080"))
start()
async function start() {
await connect();
}
async function connect() {
try {
await client.connect();
}
catch(e) {
console.error(`Failed to connect ${e}`)
}
}
async function readTodos() {
try {
const results = await client.query("SELECT ST_AsText(ST_Force2D(geom)) FROM fp ORDER BY geom <-> ST_SetSRID(ST_MakePoint("+lon+ ","+lat+ "), 3993) LIMIT 1;");
return results.rows;
}
catch(e){
return [];
}
}
req.query.lonandreq.query.latare the right way to access query parameters on that URL.