简体   繁体   English

Node-Express Rest API 中的不同 Get 方法

[英]Different Get methods in Node-Express Rest API

I have been creating a website with Mean stack and I stuck at some point.我一直在用 Mean stack 创建一个网站,但我在某个时候卡住了。 I have a mongo db database and I am currently getting each file from database (to show them on Main page) with my Rest Api which is build with Express.我有一个 mongo db 数据库,我目前正在使用我的 Rest Api 从数据库中获取每个文件(以在主页上显示它们),它是用 Express 构建的。

Server.js服务器.js

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('mongodb://username...', ['myApp']);
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

app.get('/myApp', function (req, res) {
    db.myApp.find(function (err, docs) {
        console.log(docs);
        res.json(docs);
    });
});

app.get('/myApp/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db.myApp.findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
        res.json(doc);
    })
});

app.listen(3001);
console.log('Server running on port 3001');

There is 2 get method and I can understand that because they have different parameters.有 2 个 get 方法,我可以理解,因为它们有不同的参数。 So when I call them from controllers, there is no problem because if I provide id, it will call the second get method.所以当我从控制器调用它们时,没有问题,因为如果我提供 id,它将调用第二个 get 方法。 But for example I want to use something like this in my website;但是例如我想在我的网站中使用这样的东西;

app.get('/myApp', function (req, res) {
    db.myApp.find({}).limit(2).skip(0, function(err, docs) {
        console.log(docs);
        res.json(docs);
    });
});

This get method have no parameter like the first get method in server.js but they do different jobs.这个 get 方法没有像 server.js 中的第一个 get 方法那样的参数,但它们做不同的工作。 This is limiting my search with 2 file.这限制了我对 2 个文件的搜索。 How can I use different get methods like this in my Mean Stack application?如何在我的 Mean Stack 应用程序中使用不同的 get 方法?

This is my code for calling get method from my main controller.这是我从主控制器调用 get 方法的代码。 How can I make sure to call specific get method?如何确保调用特定的 get 方法? Thanks..谢谢..

$http.get('/myApp').success(function(response) { .. });

What you want is not possible.你想要的是不可能的。 Somehow you need to distinguish between your 2 intentions, either by giving the endpoints different names (like you already suggest in your comment) or by providing for example a query parameter so you could do a call like:您需要以某种方式区分您的两个意图,通过为端点提供不同的名称(就像您在评论中已经建议的那样)或提供例如查询参数,以便您可以进行如下调用:

$http.get('/myApp?limit=2').success(function(response) { .. });

When limit is omitted, you could return all results.当省略限制时,您可以返回所有结果。

Something like:就像是:

app.get('/myApp', function (req, res) {
   var limit = req.query.limit;
   if (limit === undefined) {
      // Return everything
   } else {
      // make sure limit is some valid number
      // ... and do a mongo query limited to this number
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM