現在、NodeJを学習中です。クエリパラメータを渡す際に問題が発生しています。このスタックは、明示的、要求の約束、およびmongodbで構成されています。私が撮影しているアプリケーションのデザインは、基本的にユーザーのためのUIアプリケーションであるexpress.js
を持つことです。また、すべてのREST APIエンドポイントを定義するserver.js
と、すべてのクエリと必要なロジックを実行するrecipes.js
があります。以下はNodeJSアプリケーションを介してリクエストクエリパラメータを渡す方法
それが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ルートを持って見ることができます。
// 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
は、データベースに接続し、バックデータキックそれを取得:server.js
ファイルは、以下、上述したAPIエンドポイントを示します。
// 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.js
とrecipes.js
の両方です。 req.query.count
の値は、私が打つURLがhttp://localhost:3000/recipes?count=1
であるにもかかわらず、未定義です。 req
オブジェクトが他のアプリケーション(REST API)に持っている情報を渡して、その情報をエンドポイントに取得してデータベースを正しく照会できるようにするにはどうすればよいですか?
UIからAPIを分離して(優先)、1つのルートハンドラを使用して、その中のパラメータをチェックしてください。 – Li357
UIとAPIは別です。 UIはポート3001上でホストされ、APIはポート3000上にあります。それらを別々のNodeインスタンスで実行します。 'express.js'はポート3000でAPIを消費します。私はこれらが分かれていることを誤解していますか? –
私はそれを逃した。何が起きてる? 'uri: 'http:// localhost:3000/recipes'' - クエリ文字列を渡していません。 – Li357