2016-09-20 2 views
2

あなたは、ユーザーが検索を実行できるように、エクスプレスとアングルで作成されたアプリケーションを持っています。 URLは、実行されたばかりの検索に基づいて作成されます。したがって、あなたが "Will"の検索を実行した場合、URLはhttp://localhost.com:9000/search/query?q=Willのように見えますが、すべて正常に機能しますが、以前は/query?=の検索が行われていないことを忘れていました.またはhttp://localhost.com:9000/search/robertoなどの古いリンクは機能しなくなりました。エクスプレスで古いリンクを下位互換性にする

古いリンクを再度有効にする正しい方法は何でしょうか?

フロントエンドでJavaScriptを使用してURLで/ query?= missingを探し、検索パスの後でクエリされたテキストの前に追加する必要がありますか?

答えて

0

だけで正規表現を使用して

app.use(function(req, res, next) { 
    var searchRegEx = /\/search/g; 
    var searchedTerm = req.originalUrl.replace(searchRegEx, ''); 
    var queryPath = req.originalUrl.match(/\/query[?]q=/); 
    if(!queryPath) { 
     var regexSlash = /\//g; 
     res.redirect('query?q=' + searchedTerm.replace(regexSlash, '')); 
    } 
    else { 
     next(); 
    } 
}); 
2

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を使用することができます!

+0

あなたの解決策で問題が解決しないリダイレクト。リダイレクトは '/ search/query?= 123'を'/search/123'に向かわず、有用なコードを持っていると仮定しています。私はこの問題について別の方法を見つけました。 – fauverism

+0

私は知っている、私は彼の解決策に答える直後に、それが完全な反対であることを理解した。もしあなたが望めば私にお尻を呼んでください。質問があまり混乱しない方法で聞かれたら、私は正しく答えることができました。通常、プログラムの初期状態を定義し、達成しようとしていることに言及します。 – DiglidiDudeNG