User avatar
quarkjuggler
Posts: 169
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Javascript can't make node.js work.

Tue Jan 12, 2021 7:35 pm

When I go to Chromium browser and put "localhost:5000/list", nodejs goes thru sqlite3 and makes a nice list from my SQL file. But when I bring up the following page and click the 'Try', it puts the question, "Open xdg-open?". When I click on "Open xdg-open", it puts google page. I didn't ask for xdg, nor did I ask for google. Why can't I get my list? Thanks for your help!

Code: Select all

<!DOCTYPE html>
<html>
<!-- <body onload="mainline()"> -->
<body>
<button onclick="mainline()">Try</button>
<script>
function mainline() {
   window.open("localhost:5000/list");
};
</script>
</body>
</html>
I tried to get a static local html page, and it worked fine. I tried the URL copied from W3schools and it also worked fine. Doesn't it like me? Here's what's on the back side:

Code: Select all

// Get the required pieces:
const sqlite3 = require('/home/production/node_modules/sqlite3');
var http = require('http');

// Start the HTTP server:
var server = http.createServer(function (req, res) {   // 2 - creating server
console.log("request received: " + req.url);
// Put the first screen.
   if (req.url === '/list') {
      // Open personal database:
      let db = new sqlite3.Database('/home/production/personal.db',
      sqlite3.OPEN_READWRITE, (err) => {
         if (err) {
            throw err;
         }
      });
      let sql = 'SELECT description FROM expenses ORDER BY description';
      res.writeHead(200, { 'Content-type': 'text/html' });
      db.all(sql, [], (err, rows) => {
         if (err) {
            throw err;
         }
         rows.forEach((row) => {
            res.write("<p>" + row.description + "</p>");
         });
      });

      // Close the database connection
      db.close((err) => {     // open for closing
         if (err) {
           throw err;
         };     // end for error
      });      // end for closing
   };    // end for url='/list'
});       // end for request

server.listen(5000);

I would press charges for elder abuse, but how would they put mother nature in jail?

User avatar
quarkjuggler
Posts: 169
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Javascript can't make node.js work.

Wed Jan 13, 2021 4:49 pm

Update: I just found this web site:

https://blog.chromium.org/2008/12/secur ... pages.html

--I guess the reference to my own machine made me look like a bad guy. I shall need to come up with a different strategy. Nothing is simple these days.

Thank you for reading my post. I shall look out for more answerable questions.
I would press charges for elder abuse, but how would they put mother nature in jail?

Heater
Posts: 17131
Joined: Tue Jul 17, 2012 3:02 pm

Re: Javascript can't make node.js work.

Thu Jan 14, 2021 3:24 pm

It's a while since I did that kind of thing with node.js but after a glance at your code I think:

1) It looks like your HTTP server only responds to '/list'. Can I assume then that the initial HTML is being loaded from a "file:..."? I suspect that might cause issues and that it would be better if your page was served by the HTTP server as '/index.html'. That will require reading that index file and returning it's content in a response.

2) Life would be much easier if you used the Express web module. Express handles a lot of messy HTTP details for you and is very easy to use. For example in one or two lines of code it can be made to serve up all static files in a directory. Like your index.html for example.
https://expressjs.com/

3) With that in place you no longer have to specify "localhost" or the port number in the link to "list". You should just have:

Code: Select all

window.open("list");
Perhaps with a leading "/". I forget.
Memory in C++ is a leaky abstraction .

User avatar
quarkjuggler
Posts: 169
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Javascript can't make node.js work.

Fri Jan 15, 2021 3:30 am

Thank you for your advice. I have downloaded the "Express" and I shall see what it does. I saw the name "Express" mentioned several times on the web, so I'll need to see what all the fuss is about. I'm afraid it'll come down to just learning the HTML, CSS, and Javascript.

I wish somebody would document the topology of the web and other things. I know all about method="post" but I don't know how to invoke the program that is to interpret it. A nice "hello, world" program would be nice. It's like once I have a telephone, perhaps I can figure out what to say. W3schools just says that they'll tell me later when I study PHP. --Not another !@#$%^ language!

Have a good day and stay safe!
I would press charges for elder abuse, but how would they put mother nature in jail?

Heater
Posts: 17131
Joined: Tue Jul 17, 2012 3:02 pm

Re: Javascript can't make node.js work.

Fri Jan 15, 2021 7:14 am

On the one hand the "topology of the web" is dead simple. There are web servers, there are web clients (often browsers, often not). Their interaction is simple: The client makes a request, the server sends back a response. Every request/response is a totally separate, unrelated thing, Done. That is it.

As you know, requests and responses are made using the HTTP protocol. On the face of it a simple text based protocol. Each HTTP request/response has a header containing information about such whether it is a "GET" or "POST" or whatever request and many other things: https://developer.mozilla.org/en-US/doc ... TP/Headers. It also has a "body" containing data. The body can contain an HTML web page, JSON data, and all kind of other things, as specified by it's MIME type in the header: https://developer.mozilla.org/en-US/doc ... MIME_types

On the other hand we are now in an endless world of discovery. What are all the things we can do with HTML? How do we get our pages styled with CSS? How do we use Javascript in the browser? How do we get JS to interact with the page to change what is displayed? What about SVG? Web Assembly (WASM), Animation, Video, etc, etc, etc...

Luckily we don't need to know it all to get started. Also luckily libraries like Express take care of a lot of that messy HTTP request/response and header stuff.

Having been dabbling in all this for years I still have very little clue how to do anything with CSS. Quite frankly I don't want to know much about it. It seems like a horribly complex pigs ear to me. So I use a CSS library to deal with it. Like Bootstrap: https://getbootstrap.com/ or https://picnicss.com/

A simple "Hello World" using Node.js and Express is presented in the nice tutorial. Which progresses to GET and POST requests, file upload, cookies etc. https://www.tutorialspoint.com/nodejs/n ... mework.htm

Let us know how you get on.

Edit: PS, as you have started out with node.js you can forget all about PHP. And you won;t need Apache or Nginx. Node.js can do all of what PHP does in Javascript and as you see node.js easily makes a web server. With the huge advantage that node.js can also interact with your Pi far more easily. After all it is just a Pi application. Especially nice for interacting with GPIO and such.
Memory in C++ is a leaky abstraction .

User avatar
quarkjuggler
Posts: 169
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Javascript can't make node.js work.

Fri Jan 15, 2021 9:43 pm

Thanks, heater. Now I've got a lot to study, If I don't hear from you for awhile, that'll be fine. Your answers will make a good framework for my learning. --Looks like Express has made things simpler, and that has to be the way the future will go. I'll post again to this thread when I understand what it is that I don't understand.
I would press charges for elder abuse, but how would they put mother nature in jail?

Heater
Posts: 17131
Joined: Tue Jul 17, 2012 3:02 pm

Re: Javascript can't make node.js work.

Sat Jan 16, 2021 3:24 am

quarkjuggler wrote:
Fri Jan 15, 2021 9:43 pm
...when I understand what it is that I don't understand.
Ha, yes! That is the hard part.

Good luck with that. Let us know how you get on.
Memory in C++ is a leaky abstraction .

User avatar
quarkjuggler
Posts: 169
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Javascript can't make node.js work.

Sat Jan 16, 2021 9:52 pm

I've been buried in speculation, about whether the browser would honor a request to 'localhost' because of the security breach, but I have now stepped into the sunlight, my eyes burning with embarassment! I needed to put "http://" in fromt of the 'localhost'. Bingo, it works! Thank you for your patience!
I would press charges for elder abuse, but how would they put mother nature in jail?

Return to “Other programming languages”