0

I'm new to Web Development (including JavaScript and HTML) and have a few issues within my personal project that seem to have no clear fixes.

Overview

My project is taking input from a user on the website, and feeding it to my back-end to output a list of word completion suggestions.

For example, input => "bass", then the program would suggest "bassist", "bassa", "bassalia", "bassalian", "bassalan", etc. as possible completions for the pattern "bass" (these are words extracted from an English dictionary text file).

The backend - running on Node JS libraries

trie.js file:

/* code for the trie not fully shown */

var Deque = require("collections/deque"); // to be used somewhere

function add_word_to_trie(word) { ... }
function get_words_matching_pattern(pattern, number_to_get = DEFAULT_FETCH) { ... }

// read in words from English dictionary

var file = require('fs');
const DICTIONARY = 'somefile.txt';

function preprocess() {
    file.readFileSync(DICTIONARY, 'utf-8')
        .split('\n')
        .forEach( (item) => {
        add_word_to_trie(item.replace(/\r?\n|\r/g, ""));
    });
}

preprocess();

module.exports = get_words_matching_trie;

The frontend

An HTML script that renders the visuals for the website, as well as getting input from the user and passing it onto the backend script for getting possible suggestions. It looks something like this:

index.html script:

<!DOCTYPE HTML>
<html>

   <!-- code for formatting website and headers not shown -->
   <body>
            <script src = "./trie.js">

                function get_predicted_text() {
                    const autofill_options = get_words_matching_pattern(input.value);
                    
                    /* add the first suggestion we get from the autofill options to the user's input
                     arbitrary, because I couldn't get this to actually work. Actual version of                                                                                                     
                     autofill would be more sophisticated. */

                    document.querySelector("input").value += autofill_options[0];
                }
                
            </script>

            <input placeholder="Enter text..." oninput="get_predicted_text()">
            <!-- I get a runtime error here saying that get_predicted_text is not defined -->
   </body>
</html>

Errors I get

Firstly, I get the obvious error of 'require()' being undefined on the client-side. This, I fix using browserify.

Secondly, there is the issue of 'fs' not existing on the client-side, for being a node.js module. I have tried running the trie.js file using node and treating it with some server-side code:

function respond_to_user_input() {
    fs.readFile('./index.html', null, (err, html) => {
        if (err) throw err;

        http.createServer( (request, response) => {
            response.write(html);
            response.end();
        }).listen(PORT);

    });

respond_to_user_input();
}

With this, I'm not exactly sure how to edit document elements, such as changing input.value in index.html, or calling the oninput event listener within the input field. Also, my CSS formatting script is not called if I invoke the HTML file through node trie.js command in terminal.

This leaves me with the question: is it even possible to run index.html directly (through Google Chrome) and have it use node JS modules when it calls the trie.js script? Can the server-side code I described above with the HTTP module, how can I fix the issues of invoking my external CSS script (which my HTML file sends an href to) and accessing document.querySelector("input") to edit my input field?

  • why are you trying to run node in the client side? You run your server and client separately. Client makes an HTTP request to the server, server responds to the request, client renders the response. That's how web applications work. – sinanspd Jul 05 '20 at 18:20
  • @sinanspd Right; I actually was not "running" anything through node at first. I just wrote out my code, and ran the HTML script on Google Chrome. Seeing that the fs was not defined on the client-side, I tried running my JS file through node with HTTP (not exactly knowing the nitty-gritty detail of how that worked). – Fragondruit Jul 05 '20 at 20:03

0 Answers0