简体   繁体   中英

Node.js - Using a for loop in Express.js to achieve multi-language/region support

I'm new to Node.js and I'm still not that familiar with it and I wanted to make my website support multiple regions; eg United Kingdom and the United States.

Similarly to other websites, I'd like to separate the regions in pages like https://website.com/us and https://website.com/gb and of course, a page for worldwide view or whatever you would like to call it - https://website.com .

To do this, I came up with the idea to loop through an array with region codes and let Node.js set the page with the data it receives from each loop.

ar locals = ['', 'gb', 'us']; // An array with region codes

...

for (var i = 0; i < locals.length; i++) { // Loops through the array
    switch (locals[i]) { // A switch statement that sets up the data
        case '': // Default view (https://website.com)
            var lang = "en";
            var title = "";
            var href= "";
            break;
        case 'gb':
            var lang = "en";
            var title = " (United Kingdom)";
            var href = "gb/";
            break;
        case 'us':
            var lang = "en";
            var title = " (United States)";
            var href = "us/";
    }

    app.get("/" + href, function(request, response) {
        response.write("<!DOCTYPE HTML>");
        response.write("<html lang=\"" + lang + "\">");
        response.write("<head>");
        response.write("<base href=\"" + base + "\"/>");
        response.write("<title>Nemalp" + title + "</title>");
        ... // There are scripts below that check the html tag's lang attribute and sets up the language properly.
    });
};

So what's the problem? Well, the page seems to be in a loading state forever. It does load some of the content but it always ends up with an GET... net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK) error. Not all scripts or favicons load properly at times and my gut is telling me that what I just did would get me fired everywhere, haha... It also always loads with the United States region settings... Where is my mistake? How do I achieve my goal properly? Any kind of help or advice is highly appreciated!

After searching for the solution for a while, I found a way to set up the regions properly. The solution for the loading problem was fixed with an response.end() function after the response.write() functions, thanks to Alex D.

To load the regions properly, I added some extra arrays to the code that pretty much replace the switch() statements you can see above, along with my original array.

var href = ['', 'gb/', 'us/']; // The links that seperate the regions
var languages = ['en', 'en-GB', 'en-US']; // Used for html's lang attribute
var titles = ['', ' (United Kingdom)', ' (United States)']; // Used in the website's title

Then, I made a function that stores those response.write() commands and sets up the page itself.

function loadPageRegion(href, language, title) { // This function will be called later, in a loop, in order to load all the pages we want. Those attributes are used to set up the properties for the corresponding region.
    function loadHome(response, languages, titles) { // Stores the response.write() functions and sets the website's title, html's lang attribute, etc.
        response.set({'content-type': 'text/html; charset=utf-8' });
        response.write("<!DOCTYPE HTML>");
        response.write("<html lang=\"" + languages + "\">");
        response.write("<head>");
        response.write("<base href=\"localhost\"/>");
        response.write("<title>Nemalp" + titles + "</title>");
        // ...
        response.end();    
}

app.get("/" + href, function(request, response) { // The request
    loadHome(response, language, title); // Loading the page's content
});

}

All we need to do now is a simple loop.

for (var i = 0; i < href.length; i++) { // Iterates through the href array.
    loadHomeRegion(href[i], languages[i], titles[i]); // Calls our function with the properties for the corresponding region.
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM