fauverism fauverism - 1 month ago 5x
AngularJS Question

Make old links backwards compatible in express

You have an application created in express and angular that allows the user to perform a search. The URL is built based upon the search that was just performed. So if you perform a search on “Will” the url looks like

Everything works fine but you forgot that the app previously performed searches without the
and now all of your old links like
no longer work.

What would be the correct approach to get the old links working again?

Should you use JavaScript on the frontend to look for /query?= missing in the URL and add after the search path but before the queried text?


It'd be easier to do a redirect on the Express back-end.

Say your code for the /search/query path is initially like this :

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.");

This is probably similar to what you have. Now, here is the answer to your question, based on the initial implementation above :

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.

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"];

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.");

So from now on, every query using /search/query?q=123 will redirect towards /search/123. It even lets you use query as the search term!