0

I am currently trying to make a webapp that uses the darksky api to pull weather data. I have the basic functionality done, but when I try to run it on port 3000, I get "Cannot GET/". I am quite new to using node and backend servers as a whole so I don't know how to debug this. I will paste my code below.

    const express = require("express");
const unirest = require("unirest");
const credentials = require('./apiCredentials.json'); 
const app = express();
    app.use(express.static('/public')); // location of my index.html
    app.get('/weather', (req, res) => {
        const {lat,lon} = req.query;
        let request = unirest("GET",`https://${credentials.host}/${lat},${lon}`);
        request.query({
            lang:"en",
            units:"auto"
        });
        request.headers({
            "dark-sky.p.rapidapi.com": credentials.host,
            "466375f66bmsh72031b571ea7c30p1f704fjsnc527236c3565": credentials.apiKey
        });
        
        request.end(response => {
            if(response.error) res.status(500).end();
            const{
                summary,
                precipProbability,
                temperature,
                windSpeed,
                windBearing
            } = response.body.currently;
            res.status(200).send(
                JSON.stringify({
                    summary: summary,
                    chanceOfRain: precipProbability,
                    temp: temperature,
                    wind:{
                        speed: windSpeed,
                        bearing: windBearing
                    }
                })
            );
        });
    });
    app.listen(3000,()=>{
        console.info('Listening on port :3000');
    });
noble334
  • 1
  • 1
  • Please specify whether the application itself failed to start (listen on port 3000) or there is an error when you issue a request to the ``/weather`` endpoint. Maybe it is better to attach an output from your terminal. – Eddie Deng Feb 03 '22 at 00:42
  • My apologies! I definitely should have worded that better. I get the output Listening on port :3000. – noble334 Feb 03 '22 at 00:46
  • what happens in your console and browser if you go to `http:localhost:3000/weather`? – timsntech Feb 03 '22 at 01:59
  • Well, you can't `GET /` because you never defined a route. Try adding this line: `app.get("/", (req, res) => res.sendFile(__dirname + "/public/index.html"));` and see what happens. – code Feb 03 '22 at 02:17

0 Answers0