Error: Invalid URI while requesting API in nodejs

Error: Invalid URI while requesting API in nodejs

您好,我是网络编程新手,我想练习 javascript 的一些 API 用法。 我试图请求基本的 openwatcher API 调用:

API call:

api.openweathermap.org/data/2.5/weather?q={city name}

示例:

var request = require('request');
request("api.openweathermap.org/data/2.5/weather?q=London",function(error,response,body){
    if(!error && response.statusCode == 200){
        console.log(body)
    }
    else{
        console.log(error);
        console.log(response.statusCode)
    }
})

但是输出错误

Error: Invalid URI "api.openweathermap.org/data/2.5/weather?q=London"

这可能是什么问题?

看起来 request 要求您在 URL 中指定协议。
如果您在 URL 前加上 http://(或者,如果服务支持,理想情况下,https://),它应该开始工作。