如何通过NodeJS应用程序传递请求查询参数

时间:2017-07-31 17:10:01

标签: javascript node.js mongodb express

我目前正在研究NodeJs,并且遇到了传递查询参数的问题。堆栈由express,request-promise和mongodb组成。我正在拍摄的应用程序的设计是express.js基本上是用户的UI应用程序。还有一个server.js定义了任何REST api端点,还有一个recipes.js来处理所有查询以及我想要的任何逻辑。

以下是我之前提到的处理UI的express.js文件:

// Node imports.
const express = require('express');
const app = express();
const request = require('request');
const rp = require('request-promise');
const url = require('url');

const port = 3001;

// View configuration information.
app.use(express.static('public'));
app.set('view engine', 'ejs');

// Routing.
app.get('/recipes', (req, res) => {
    console.log("TODO(map) In the express.js: " + req.query.count);

    var options = {
    method: 'GET',
    uri: 'http://localhost:3000/recipes',
    json: true
    }

    // Run get and wait on promise before rendering.
    rp(options, (req, res) => {
    }).then((recipes_list) => {
    res.render('index-promise', { pageTitle: 'Recipes', recipes: recipes_list })
    });
})

// Server listening.
app.listen(port, () => {
    console.log('Server listening on http://localhost:' + port);
});

您现在可以看到应用程序只有一个URL路由。下面的server.js文件显示了上面提到的API端点:

// Node imports.
const url = require('url');
const express = require('express');
const app = express();

const port = 3000;

// API imports.
var recipeApi = require('./apis/recipes.js');

// Routing.
app.get('/recipes', (req, res) => {
    console.log("TODO(map) In the server.js: " + req.query.count);
    recipeApi.getRecipesByCount(req, res);
})

// Server listening.
app.listen(port, () => {
    console.log('Server listening on http://localhost:' + port);
});

最终文件recipes.js连接到数据库并将数据踢回来。

// Node imports.
const url = require('url');
const express = require('express');
const app = express();

// Mongo imports.
const MongoClient = require('mongodb').MongoClient;
const Db = require('mongodb').Db
const Server = require('mongodb').Server

// Create a DB object so we can query the db anywhere.
const db = new Db('mydb', new Server('localhost',27017));

/**
 * Function for getting a certain number of recipes.
 * NOTE: The req object should include a "count" query parameter.
 * 
 * req: Request being passed in from the server.js
 * res: Response that will be sent back.
 *
 */
exports.getRecipesByCount = (req, res) =>
{
    console.log("TODO(map) In the recipes.js: " + req.query.count);

    if (!req.query.count)
    {
    req.query.count = 0
    }
    db.open((err, db) => {
    db.collection("recipes").find({}).limit(parseInt(req.query.count)).toArray((err, result) => {
        if (err) throw err;
        res.setHeader('Content-Type', 'application/json');
        res.send(result);
        db.close();
    });
    });

}

所以在最后,我遇到的问题是在console.log调用server.jsrecipes.js。尽管我正在点击的网址是req.query.count,但http://localhost:3000/recipes?count=1值仍未定义。我应该如何将req对象的信息传递给另一个应用程序(REST API),以便我可以将这些信息提供给终点并正确查询数据库?

1 个答案:

答案 0 :(得分:0)

您似乎在端口@Qualifier上向后端API发送请求而没有查询参数。在您的express.js文件中,您有:

3000

如您所见,其中没有查询参数。你需要在这里添加参数。如果您将其更改为下面的代码,则应该有效:

var options = {
method: 'GET',
uri: 'http://localhost:3000/recipes',
json: true
}