Home > Blockchain >  I want all my routes to work but for some reasons some are working and others are giving me 313 erro
I want all my routes to work but for some reasons some are working and others are giving me 313 erro

Time:12-06

I am using an API for my weather app

Here's my code

const express = require('express');
const cors = require('cors')
const request = require('request');
const PORT = 3060;
const app = express()

app.use(cors());
app.use(express.json());

The starting two endpoints are working fine (weather and location). They are sending the JSON to the web page

app.get('/weather',(req,res) => {
let city  = req.query.city;
const request = require('request');
request(`http://api.weatherapi.com/v1/current.json?key={myKEy}&q=${city}&aqi=no`,
    function (error, response, body) {
        console.log(body)
        let data = JSON.parse(body);
        if(response.statusCode === 200){
            res.send(data.current.condition.text)
        }
    });
})

app.get('/location',(req,res) => {
    let city  = req.query.city;
    const request = require('request');
    request(`http://api.weatherapi.com/v1/current.json?key={myKEy}&q=${city}&aqi=no`,
        function (error, response, body) {
            console.log(body)
            let data = JSON.parse(body);
            if(response.statusCode === 200){
                res.send(data.location.name)
            }
    });
})

Now the rest are keep giving me errors 313(Invalid Store):

{"location":{"name":"Nanital","region":"Uttarakhand","country":"India","lat":29.38,"lon":79.45,"tz_id":"Asia/Kolkata","localtime_epoch":1670136364,"localtime":"2022-12-04 12:16"},"current":{"last_updated_epoch":1670136300,"last_updated":"2022-12-04 12:15","temp_c":25.2,"temp_f":77.4,"is_day":1,"condition":{"text":"Sunny","icon":"//cdn.weatherapi.com/weather/64x64/day/113.png","code":1000},"wind_mph":2.2,"wind_kph":3.6,"wind_degree":229,"wind_dir":"SW","pressure_mb":1014.0,"pressure_in":29.95,"precip_mm":0.0,"precip_in":0.0,"humidity":30,"cloud":0,"feelslike_c":24.8,"feelslike_f":76.6,"vis_km":10.0,"vis_miles":6.0,"uv":7.0,"gust_mph":1.3,"gust_kph":2.2}}
express deprecated res.send(status): Use res.sendStatus(status) instead Server.js:61:21
node:_http_server:313
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 30
    at new NodeError (node:internal/errors:393:5)
    at ServerResponse.writeHead (node:_http_server:313:11)
    at ServerResponse._implicitHeader (node:_http_server:304:8)
    at ServerResponse.end (node:_http_outgoing:993:10)
    at ServerResponse.send (E:\weatherapp\node_modules\express\lib\response.js:232:10)
    at Request._callback (E:\weatherapp\Server.js:61:21)
    at self.callback (E:\weatherapp\node_modules\request\request.js:185:22)
    at Request.emit (node:events:513:28)
    at Request.<anonymous> (E:\weatherapp\node_modules\request\request.js:1154:10)
    at Request.emit (node:events:513:28) {
  code: 'ERR_HTTP_INVALID_STATUS_CODE'
}
Node.js v18.10.0
[nodemon] app crashed - waiting for file changes before starting...

I re-checked my JSON path several times and they are correct but still the problem is same

app.get('/humidity',(req,res) => {
    let city  = req.query.city;
    const request = require('request');
    request(`http://api.weatherapi.com/v1/current.json?key={myKEy}&q=${city}&aqi=no`,
        function (error, response, body) {
            console.log(body)
            let data = JSON.parse(body);
            if(response.statusCode === 200){
                res.send(data.current.humidity)
            }
    });
}) 

app.get('/temprature',(req,res) => {
    let city  = req.query.city;
    const request = require('request');
    request(`http://api.weatherapi.com/v1/current.json?key={myKEy}&q=${city}&aqi=no`,
        function (error, response, body) {
            console.log(body)
            let data = JSON.parse(body);
            if(response.statusCode === 200){
                res.send(data.current.temp_c)
            }
    });
}) 

app.get('/wind',(req,res) => {
    let city  = req.query.city;
    const request = require('request');
    request(`http://api.weatherapi.com/v1/current.json?key={myKEy}=${city}&aqi=no`,
        function (error, response, body) {
            console.log(body)
            let data = JSON.parse(body);
            if(response.statusCode === 200){
                res.send(data.current.wind_kph)
            }
    });
}) 

app.listen(
    PORT , () => console.log(`This is Listing to port ${PORT}`)
)

CodePudding user response:

res.send() consider int value as status code. Though try passing value through strings.

app.get('/wind',(req,res) => {
    let city  = req.query.city;
    const request = require('request');
request(`http://api.weatherapi.com/v1/current.json?key={myKEy}=${city}&aqi=no`,
    function (error, response, body) {
            console.log(body)
            let data = JSON.parse(body);
            if(response.statusCode === 200){
                let windV = "The wind speed is : " data.current.wind_kph;
                res.send(windV);
            }
});
}) 

  • Related