When making an ajax call to the NodeJS server (not localhost), it doesn't return anything. When making the ajax call, NodeJS shows the results inside my NodeJS console (ssh). NodeJS crashes 10-15s after the ajax call with an error.
I've tried to use Pool but I don't understand it.
frontend
"use strict";
var btn = document.getElementById('button');
function ajax(){
let req = new XMLHttpRequest();
req.onreadystatechange = function(){
if( this.readyState === 4 && this.status === 200){
let data = JSON.parse( this.responseText );
console.log(data);
}
};
req.open("GET", "/ajax/", true);
req.send();
};
btn.addEventListener("click", (e)=>{
e.preventDefault();
ajax();
});
backend (nodejs)
const {Client} = require('pg');
const db = new Client({
user: "x",
password: "x",
host: "x",
port: 123,
database: "abc",
ssl: true
});
module.exports = {
async select( sql ){
try{
await db.connect();
console.log("Connected to DB");
const result = await db.query( sql );
console.table(result.rows); // <-- Shows the data i want to send back.
return JSON.stringify(result.rows);
}
catch(ex){
console.log("We messed up! " + ex);
}
finally{
await db.end();
console.log("DB connection closed");
}
}
};
I expect the ajax call to return data from the postgres database. The same data i see inside my nodeJS console.
Error: Client has already been connected. You cannot reuse a client.
events.js:177
throw er; // Unhandled 'error' event
^
Error: This socket has been ended by the other party
at TLSSocket.writeAfterFIN [as write] (net.js:407:14)
at Connection.end
code: 'EPIPE'