我想构建一个 API。我的应用程序是一个meteor/nodejs应用程序。因此,我在 public
文件夹中创建了一个 api.js
文件:
document.write({ example: 'Lorem ipsum' });
我希望通过调用 localhost:3000/api.js
来获取对象作为输出,但我看到的是源代码。
第二个问题:
如何处理参数?例如localhost:3000/api.js?type=article
const type = get[type]; // should be 'article' in this example
使用路由将呈现 HTML 内容,但我需要输出纯 JSON 内容...
最佳答案
Meteor 的工作方式不同,你不做 document.write,而是使用模板做事。
为了提供 api,您可以使用诸如 Restivus https://atmospherejs.com/nimble/restivus 之类的包。
这将允许您非常轻松地创建正确构造的 API,并提取您想要的 URL 参数。
关于用于构建 API 的 JavaScript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652668/