我仍然对node.js还是陌生的,我正在尝试让我的小型API返回数据。如您所见here
该代码在此在线编辑器上可以完美运行,但是在本地运行节点时会出错。
我的代码:
var http = require('http');
var options = { method: 'GET',
url: 'https://eftelingapi.herokuapp.com/attractions',
headers:
{'cache-control': 'no-cache'};
request(options, function (error, response, body) {
if (error) throw new Error(error);
var jsonData = JSON.parse(body);
console.log(jsonData.AttractionInfo[0]);
});
错误:
ReferenceError: request is not defined
at Object.<anonymous> (/Users/ricky/Desktop/eftelApp/app.js:9:1)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
at startup (internal/bootstrap/node.js:308:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)
希望有人知道我的代码有什么问题,或者我可以在本地节点上解决什么问题?我已经安装了“请求”包,但是似乎没有做到
答案 0 :(得分:0)
尝试
state_dic= {1: "state 1", 2: "state 2", 3: "state 3"}
state_time_dic = {"state 1": 5, "state 2": 10, "state 3": 8}
def mulit_state_system(resource):
i = random.randint(1,4)
if i in state_dic.keys:
state = state_dic[i]
with resource.request as req:
yield req
yield timeout(state_time_dic[state])
检查this了解更多信息。