S
S
Stanislav2021-06-12 20:57:17
Node.js
Stanislav, 2021-06-12 20:57:17

How to make the code when fetching headers more correctly?

Hello everyone, maybe someone has already had a similar one and will tell you how it is easier to implement everything.

I have a search on my site that has additional settings, such as take into account the user, section, etc.
Since these pages are in fact different, it would be foolish to prohibit search engines from indexing them, so I want to create unique titles for them.
There are GET requests
1. SEARCH - search word
2. CATALOG - document section
3. USER - document user

It was decided to make a certain key from get requests by which to select the title from the language file
This is how I create the key

function getPageTitleKey(request) {
    return o = ["catalog", "user", "search"].filter(e => Object.keys(request).includes(e)), o.join(".")
}

As a result, I get various variations of the key
1. search
2. catalog
3. user
4. search.catalog
5. search.user
6. catalog.user
7. catalog.search.user

This helps to select the correct title from the language file for subsequent work with it
//ru.js
//....
titles: { 
        "catalog": {
            name: "CATALOG ...."
        },
        "search": {
            name: "SEARCH ....."
        },
        "user": {
            name: "....  USER"
        },
        "catalog.search": {
            name: "SEARCH CATALOG ......"
        },
        "catalog.user": {
            name: "CATALOG от пользователя USER"
        },
        "user.search": {
            name: "SEARCH ..... добавленные пользователем USER"
        },
        "catalog.user.search": {
            name: "SEARCH CATALOG от пользователя USER"
        }
    }
//....


The problem itself lies in the scale, because in the handler itself (search.js) I need to create headers through the switch
switch(page_title_key) {
 case "search": return isNameSearch(request)
 case "catalog": return isNameCatalog(request)
 case "catalog.user": return isNameCatalogUser(request)
 //....
}

Well, already in the function itself, I replace the necessary value using replace.
In fact, the search.js file is inflated, of course this is tolerable, but not pleasant at all.

It would be great to create midleware and, when accessing the search page, prepare data for a selection of page headers, but the problem is that the headers contain various data that I get from the database.
I’m wondering if it’s possible to somehow implement this using middleware, for example, which will spit out a function with settings in the request, for example

request.titles = function(e, key) {
 return lang[key].name.replace("USER" e.user)
}

And in search.js to get this function to make simpler settings for passing data to request.titles to get the desired result?
Or, what are the alternatives? Any thoughts on the topic would be interesting.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question