I use browserify to let me use require()
in my javascript code but it generates code that contains net.Socket() which give Uncaught TypeError: net.Socket is not a constructor. in the browser
is there any solution for that?
node version v14.17.4
browserify 17.0.0
this is my main code
tryjson.js
const axios = require('axios');
const InsertToDataBase = require("./InsertToDataBase");
const username = 'admin'
const password = 'admin'
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const urlLAMP_0 = 'http://127.0.0.1:8282/~/mn-cse/mn-name/LAMP_0/DATA/la'
const urlLAMP_1 = 'http://localhost:8282/~/mn-cse/mn-name/LAMP_1/DATA/la'
function getDataLAMP_0(){
axios.get(urlLAMP_0, {
headers: {
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin':'*',
"X-M2M-RI":"OM2M-webpage",
'Authorization': `Basic ${token}`,
'Accept': 'application/json',
'mode': 'cors',
'credentials': 'include',
}
})
.then(function(response) {
document.getElementById("rn0").textContent = response.data['m2m:cin'].rn;
document.getElementById("ty0").textContent = response.data['m2m:cin'].ty;
document.getElementById("ri0").textContent = response.data['m2m:cin'].ri;
document.getElementById("pi0").textContent = response.data['m2m:cin'].pi;
document.getElementById("ct0").textContent = response.data['m2m:cin'].ct;
document.getElementById("lt0").textContent = response.data['m2m:cin'].lt;
document.getElementById("st0").textContent = response.data['m2m:cin'].st;
document.getElementById("cnf0").textContent = response.data['m2m:cin'].cnf;
document.getElementById("cs0").textContent = response.data['m2m:cin'].cs;
document.getElementById("con0").textContent = response.data['m2m:cin'].con;
return response;
})
.then((response) => {
var rn0 = response.data["m2m:cin"].rn;
var ty0 = response.data["m2m:cin"].ty;
InsertToDataBase.insertdatatolamp0(0,1);
})
}
function getDataLAMP_1(){
axios.get(urlLAMP_1, {
headers: {
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin':'*',
"X-M2M-RI":"OM2M-webpage",
'Authorization': `Basic ${token}`,
'Accept': 'application/json',
'mode': 'cors',
'credentials': 'include',
}
})
.then(function(response) {
document.getElementById("rn1").textContent = response.data['m2m:cin'].rn;
document.getElementById("ty1").textContent = response.data['m2m:cin'].ty;
document.getElementById("ri1").textContent = response.data['m2m:cin'].ri;
document.getElementById("pi1").textContent = response.data['m2m:cin'].pi;
document.getElementById("ct1").textContent = response.data['m2m:cin'].ct;
document.getElementById("lt1").textContent = response.data['m2m:cin'].lt;
document.getElementById("st1").textContent = response.data['m2m:cin'].st;
document.getElementById("cnf1").textContent = response.data['m2m:cin'].cnf;
document.getElementById("cs1").textContent = response.data['m2m:cin'].cs;
document.getElementById("con1").textContent = response.data['m2m:cin'].con;
})
}
getDataLAMP_0();
getDataLAMP_1();
setInterval(getDataLAMP_0,100);
setInterval(getDataLAMP_1,100);
InsertToDataBase.js
const {Client} = require('pg')
const client = new Client({
user:"postgres",
password:"admin",
host:"localhost",
port:"5432",
database:"postgres",
})
function insertdatatolamp0(rn0,ty0){
client.connect()
.then(()=>console.log("connected successfuly"))
.then(()=>client.query("insert into lamp0 values ($1,$2)",[rn0,ty0]))
.catch(e=> console.log(e))
.finally(()=> client.end())
}
module.exports = { insertdatatolamp0 };
if i understood correctly, you have two endpoints serving json data and then you will feed a postgres database with it.
however, the InsertToDataBase.js
tries to use the postgre sql driver on browser, therefore the error you are seeing.
that code will run fine on node, but not on browser.
since tryjson.js
seems to feed a html document while consumes the json endpoints, you either give up the database part or create a small node server to handle those inserts for you.