Expressバックエンドでリダイレクトする方が簡単です。
app.get("/search/query", function (req, res) {
// Do your query validation and fetch your search result.
// Here, I just check if a query value was given or not for the q param.
// I recommend you use better ways to check for empty queries.
// (ex: lodash's `isEmpty()` function)
if (req.query.q) {
// Serve the page !
res.send("What you want to render if the search result finds something.");
}
else {
// Return an error !
res.status(404).send("Nothing was found with the criterias entered.");
}
});
これはおそらく、あなたが持っているもののようになります。
は/search/query
パスのためのあなたのコードは次のように最初は言います。だから、今から、/search/query?q=123
を使用して、すべてのクエリが/search/123
の方にリダイレクトします
app.get("/search/query", function (req, res, next) {
// Check if a query value was given AND if the value isn't equal to "query".
// The later condition is to prevent infinite loops.
if (req.query.q && req.query.q !== "query") {
// Redirect using the value assigned to the q query param.
res.redirect("/search/" + req.query.q);
}
else {
// Since there is no query parameter named `q` in the request,
// we can be sure that `query` reffers to a search term.
next();
}
});
app.param("srchterm", function (req, res, next, value) {
// Check, for example, if the value isn't empty.
if (value) {
// Do your query validation and fetch your search result HERE.
// Add those results in an array in the res.locals object.
// Those results can be used later.
res.locals.results = ["all", "your", "search", "results"];
}
next();
});
app.get("/search/:srchterm", function (req, res) {
console.log("another blah");
// We don't need to fetch the data here anymore, since it's handled by the param parser above!
// However, it's still necessary to check if the search gave back some results.
if (res.locals.results) {
// Serve the results !
res.send("A total of " + res.locals.results.length + " results were found for " + req.params['srchterm']);
}
else {
// Return an error !
res.status(404).send("Nothing was found with the criterias entered.");
}
});
:さて、ここであなたの質問への答えは、上記の初期の実装に基づいて、あります。それはまた、検索用語としてquery
を使用することができます!
あなたの解決策で問題が解決しないリダイレクト。リダイレクトは '/ search/query?= 123'を'/search/123'に向かわず、有用なコードを持っていると仮定しています。私はこの問題について別の方法を見つけました。 – fauverism
私は知っている、私は彼の解決策に答える直後に、それが完全な反対であることを理解した。もしあなたが望めば私にお尻を呼んでください。質問があまり混乱しない方法で聞かれたら、私は正しく答えることができました。通常、プログラムの初期状態を定義し、達成しようとしていることに言及します。 – DiglidiDudeNG