Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于构建API的JavaScript文件_Javascript_Node.js_Api_Meteor - Fatal编程技术网

用于构建API的JavaScript文件

用于构建API的JavaScript文件,javascript,node.js,api,meteor,Javascript,Node.js,Api,Meteor,我想建立一个API。我的应用程序是meteor/nodejs应用程序。因此,我在public文件夹中创建了一个api.js文件: document.write({ example: 'Lorem ipsum' }); 我希望通过调用localhost:3000/api.js将对象作为输出,但我看到的是源代码 第二个问题: 如何处理参数?e、 g.localhost:3000/api.js?type=article const type = get[type]; // should be 'ar

我想建立一个API。我的应用程序是meteor/nodejs应用程序。因此,我在
public
文件夹中创建了一个
api.js
文件:

document.write({ example: 'Lorem ipsum' });
我希望通过调用
localhost:3000/api.js
将对象作为输出,但我看到的是源代码

第二个问题:

如何处理参数?e、 g.
localhost:3000/api.js?type=article

const type = get[type]; // should be 'article' in this example

使用路由将呈现HTML内容,但我需要输出纯JSON内容…

Meteor的工作方式不同,您不需要编写文档。编写时,您需要使用模板

为了提供api,您可以使用诸如Restivus之类的包


这将允许您非常轻松地创建正确构造的API,并提取所需的URL参数。

在meteor
public
文件夹中,仅用于服务静态资产,这意味着文件“按原样”服务。它并不用于创建api或任何类型的动态路由。若你们想建立一个REST api签出。你们是说REST api吗?据我所知,仅仅获得一个集合端口作为响应是不可能的(使用restivus),不是吗?我的意思是,我不想将完整的集合发送到api,而是过滤集合以获取一些文档……您可以公开完整的集合,或者编写自定义处理程序来进行过滤(例如在用户ID上)