0

I have an express.js server that makes a HTTP GET Request to retrieve weather data. In my JavaScript, I obtain the latitude and longitude variables. I need to send those variables to my express.js server using fetch. How can I do this?

I have tried sending the query parameters as the body of the request object, but I learned GET requests cannot do that.

Express Server Code:

app.get("/data", (req, res) => {
url  =  `http://api.openweathermap.org/data/2.5/uvi?appid=${API}&lat=${latitude}&lon=${longitude}`;

axios
    .get(url)
    .then(response  => {
        res.send(response.data);
    })
    .catch(error  => {
        console.log(errorx);
    });
});

How would I get the latitude and longitude variables from my JavaScript?

I expect the express server to have the required variables and perform the GET request.

0

2 Answers 2

2

Query string parameters are part of the request URL.

Try the following to pass latitude and longitude when you do a GET request assuming the endpoint is http://localhost:3000/data?lat=0&long=0

app.get("/data", (req, res) => {
  // Add this to retrieve query string values
  let latitude = req.query.lat;
  let longitude = req.query.long;

  // Rest of your code
});
Sign up to request clarification or add additional context in comments.

Comments

2

I'll share with you an example I've done before. works for me.

/server.js

var http = require('http');
var fs = require('fs');

var express = require('express');
var app = express();
var path = require('path');

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.use('/app', express.static(__dirname + '/app'));
app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/app/index.html'));//my app page , may be different for you
});

var routes = require('./scripts/routes'); 
routes(app); 

app.listen(8080);

console.log('-> Port : 8080');

/scripts/routes.js

'use strict';


module.exports = function (app) {
    var todoList = require('../scripts/controller');

    app.route('/company/:companyId')
        .get(todoList.getInfo)

    app.route('/product-add/:name/:meter')
        .get(todoList.addPro);    

};

/scripts/controller.js

'use strict';

var sql = require("mssql");//I used mssql. may be different for you
var request = new sql.Request();

exports.getInfo = function (req, res) {


var comId = req.params.companyId

request.query('select * from company where id='+comId , function (err, recordset) {

    if (err) console.log(err)

    res.json(recordset);

});


};

exports.addPro = function (req, res) {

   // req.params.name 
  // req.params.meter

};

query address : http://127.0.0.1:8080/company/15

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.