用于构建 API 的 JavaScript 文件

标签 javascript node.js api meteor

我想构建一个 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/

相关文章:

javascript - Alert On input Field 在表单中点击 3 次

widget - UserVoice 等托管服务如何将其内容嵌入其他网站?

javascript - 代码在生产环境和本地环境中表现不同

arrays - 如何干净地列出数组中的项目 - Mongoose

javascript - 根据 promise 结果运行逻辑

node.js - Sequelize : building and saving an object that contains an array

javascript - 自动检测 highlight.js 中为动态添加的元素添加的类

api - 授权 Facebook 将用户与其他第三方联系起来

api - Firefox WebAudio API

api - TokBox OpenTok 替代方案?