Javascript 使旧链接在express中向后兼容
您有一个在express和angular中创建的应用程序,允许用户执行搜索。URL是基于刚才执行的搜索生成的。所以如果你在“Will”上搜索url看起来像Javascript 使旧链接在express中向后兼容,javascript,angularjs,express,Javascript,Angularjs,Express,您有一个在express和angular中创建的应用程序,允许用户执行搜索。URL是基于刚才执行的搜索生成的。所以如果你在“Will”上搜索url看起来像http://localhost.com:9000/search/query?q=Will一切正常,但您忘记了应用程序以前在没有/query?=的情况下执行搜索,现在您所有的旧链接,如http://localhost.com:9000/search/will或http://localhost.com:9000/search/roberto不再工
http://localhost.com:9000/search/query?q=Will
一切正常,但您忘记了应用程序以前在没有/query?=
的情况下执行搜索,现在您所有的旧链接,如http://localhost.com:9000/search/will
或http://localhost.com:9000/search/roberto
不再工作
让旧链接重新工作的正确方法是什么
您是否应该在前端使用JavaScript来查找/query?=URL中缺失的内容,并在搜索路径之后、查询文本之前添加?在Express后端执行重定向会更容易 假设
/search/query
路径的代码最初是这样的:
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.");
}
});
这可能与您拥有的类似。现在,根据上述初步实施情况,以下是您问题的答案:
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.");
}
});
因此,从现在起,使用
/search/query?q=123
的每个查询都将重定向到/search/123
。它甚至允许您使用query
作为搜索词 只需使用正则表达式并重定向即可
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();
}
});
您的解决方案无法解决此问题。重定向应该转到
/search/query?=123
,而不是指向/search/123
,而您确实有有用的代码。我找到了解决这个问题的另一种方法。我知道,在回答了他的解决方案后,我发现情况完全相反。如果你想叫我混蛋,但我相信如果这个问题问得不那么让人困惑,我可能会回答正确;通常,您定义程序的初始状态,然后提及您试图实现的目标,而不是相反。