1

I am unable to successfully send a SQL query from a served javascript file back to the server so that the server can process it. I intend on sending the result back to the javascript to do some conditional graphics work with p5.js. There will only ever be one user connected to the server at a time (me)

I am very in the dark when it comes to server structure and web development so I haven't even known where to start. Any chances at research have been shot down by a lack of results. I really just need the ability to store things to a database and query with SQL and do graphics work based on the result of the queries. As such I have tried moving to processing-java but the only library that allows for DB communication with SQL is horribly out of date and does not work with the current version of MySQL.

The actual server code is as follows:

const http = require('http');
const path = require('path');
const fs = require('fs');
const mysql = require('mysql');

let connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root"
});

function handleRequest(req, res) {
  console.log(req.url);
  let pathname = req.url;
  if (pathname == '/') {
    pathname = '/index.html';
  }
  let ext = path.extname(pathname);

  const typeExt = {
    '.html': 'text/html',
    '.js':   'text/javascript',
  };
  let contentType = typeExt[ext] || 'text/plain';

  fs.readFile(__dirname + pathname,
    function (err, data) {
      if (err) {
        res.writeHead(500);
        return res.end('Error loading ' + pathname);
      }
      res.writeHead(200,{ 'Content-Type': contentType });
      res.end(data);
    }
  );
}

let server = http.createServer(handleRequest);
server.listen(8080);

console.log('Server started on port 8080');

and it serves a p5 javascript sketch and an html file.

The current script can connect to the MySQL server I have running, and can serve the p5 script properly but I can't get the connection between them. Any help would be greatly appreciated.

Jdev52
  • 11
  • 2

0 Answers0