我只想在此链接https://quotes.rest/qod.json 中获取一些特定的引号,但它不起作用。我复制了本教程https://youtu.be/wohjl01HZuY 中的代码,但它对我不起作用。我想从 json 链接获得的报价不适用于下面的函数。任何人都可以帮助我吗?:)
exports.handler = function(context, event, callback) {
const fs = require('fs');
fs('https://quotes.rest/qod.json').then(response =>{
const qotd = JSON.parse(response.body);
let quote = qotd.contents.quotes[0];
callback(null,quote);
});
};
Twilio 开发人员布道者在这里。
正如 user3761325 指出的那样,fs
是 Node.js 文件系统模块,不会发出 HTTP 请求。有许多库可以轻松地在 Node.js 中发出 HTTP 请求,我个人喜欢got
.
创建此视频时,该got
模块实际上在 Twilio Functions 中默认可用,但现在已不再如此。您需要安装got
到函数依赖项中。您可以在此处查看有关向 Twilio 函数添加依赖项的文档。
添加got
到依赖项后,您可以要求got
并使用它来发出视频中的请求:
const got = require("got");
exports.handler = function(context, event, callback) {
got('https://quotes.rest/qod.json').then(response =>{
const qotd = JSON.parse(response.body);
let quote = qotd.contents.quotes[0];
callback(null, quote);
});
};
你可以用node-fetch
or做同样的事情axios
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句