nodeJs connection with MySql for androidStudio - javascript

i'm trying to connect Mysql with a files java-script by nodejs.
But i want my server apache send my new data automatically to my app android.
For this I have a table in my database who have for name antenne who return the differents values of this table.
I have already do a script php for connect my app android and my database but with a script php isn't a callback server
Indeed i want my server push the data in the app android and not the app android who ask the data to my server
My script java
var express =require('express');
var mysql=require('mysql');
var app=express();
var connect = mysql.createConnection({
//properties...
host: 'localhost',
user:'root',
passeword;'root',
database'Antenne',
});
connection.connect(function(error){
if(!!error){
console.log('Error');
}else{
console.log('Connected');
});
app.get('/',function(req,resp){
//aboutmysql
connection.query("SELECT *FROM antenne"function(error,rows,fields))
if(!!error){
console.log('Error in the query');
}else{
console.log('Sucessful query');
});
app.listen(80);

Related

React: Trying to connect index.js to database but its not working

So i built a frontend where you can fill in a movie name, a review and submit it to a database. Now im trying to connect a mysql database i created to the index.js , so that it gets filled with the first entry. Im trying to accomplish it like this:
const express = require('express');
const app = express();
const mysql = require('mysql');
const db = mysql.createPool({
host: "localhost",
user: "root",
password:"password",
database:'CRUDDatabase',
});
app.get('/', (req, res) => {
const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview) VALUES (1,'inception', 'good movie');"
db.query(sqlInsert, (err, result) =>{
res.send("change done");
});
})
app.listen(3001, () => {
console.log("running on port 3001")
})
But somehow the frontend gets the text ive send "Change done" but the database still doesnt show any entries. Any ideas where my mistake may be? Is it a code mistake or does it have to do with me db configuration. In mysql workbench i just created a default connection without changing anything.
EDIT: The Error seems to be the following:
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
EDIT:
The following comment here solved my problem:
Execute the following query in MYSQL Workbench ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without #'localhost' part.
I think you have an error in your code but you are not showing it as you don't test in err variable, try this code in order to show what error you are getting:
const express = require('express');
const app = express();
const mysql = require('mysql');
const db = mysql.createPool({
host: "localhost",
user: "root",
password:"password",
database:'CRUDDatabase',
});
app.get('/', (req, res) => {
const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview) VALUES (1,'inception', 'good movie');"
db.query(sqlInsert, (err, result) =>{
if(err) {
console.log(err);
res.send(err.toString());
}
res.send("change done");
});
})
app.listen(3001, () => {
console.log("running on port 3001")
})
So as Med Amine Bejaoui pointed out in a comment, the solution is:
Execute the following query in MYSQL Workbench ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without #'localhost' part.

confusion about how connection.end() works in node.js mySQL module

const mysql = require('mysql');
let connection = mysql.createConnection(...);
connection.connect((err)=>{
...
connection.query((err)=>{
...
connection.end();});
});
After I close the connection by using
connection.end()
, if I want to query the database again using the same credentials, do I need to make a new connection by calling
mysql.createConnection(...)
Or can I reuse the same connection by simply calling
connection.connect(...)
A little background: I'm deploying an angular/node.js app to a shared hosting website, and the web host has a maximum limit of 25 concurrent connections to mySQL database, therefore I need to make sure I close a connection properly after a user does his query. I am not sure if I could reuse the connection created by mysql.createConnection(...) after I close that connection, or do I need to create a brand new connection.
You can use one global connection for getting data from db .
If You working on single file than you can write as
app.js one file only
var mysql = require('mysql');
var connection = mysql.createConnection(...);
connection.query('SELECT 1', function (error, results, fields) {
if (error) throw error;
// connected!
});
if you want to use same connection in multiple file than you can write as
app.js
app.use(
connection(mysql, {
host: xxxxx,
user: 'root',
password : xxxx,
port : 3306,
database:dbname
},'pool'),
);
var oem = require('./routes/type');
app.get('/api/oemtype',oem.type);
For the second file
type.js
exports.type = function(req, res){
req.getConnection(function(err,connection){
var query = connection.query('SELECT * FROM type',function(err,rows)
{
if(err)
res.json({
status:0
});
res.send(rows);
res.render('customers',{page_title:"Customers - Node.js",data:rows});
});
});
};
No need to use of connection.end().

AJAX - Using ajax to call mySQL data with Node.js and Express

I am new to programming for Web Applications
I am making a web service app using AJAX, mySQL, Node.js and Express.
I have made a database for mySQL and have connected it.
The problem i am facing is using AJAX to call mySQL data and to display it on my web-page using Node.JS / Express.
The code i have for the server and database connection:
app.js
var express = require('express');
var http = require('http');
var path = require('path');
//Import mySQL module
var mysql = require('mysql');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
//Connection object using credentials
var con = mysql.createConnection({
host: "localhost",
user: "data",
password: "database",
database: "mydb"
});
//Connecting to the database
con.connect(
function(err){
if (err) throw err;
console.log("Database Connected");
}
);
con.end();
app.use(express.static(path.join(__dirname, '/public')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(8080);
I will be creating a new js file to contain my AJAX code, however i do not know how to start constructing it. My aim is to use Node.js/Express to call information from my database into a table which i have formed using Bootstrap
Thanks

node.js Cannot GET /socket.io

I have the following app with express/socket.io (the app is listening and live without any errors).
When I http-request it I get the following error:
GET http://xxxxxxx.com:3035/socket.io/1/?t=1449090610579 400 (Bad Request)
And on the socket.io reponse at http://xxxxxxx.com:3035/socket.io I get:
Cannot GET /socket.io
app.js:
var express = require('express'),
http = require('http'),
sio= require('socket.io'),
fs=require('fs'),
app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'xxxxxxx',
password: 'xxxxxxx',
database: 'xxxxxxxxx'
}
);
connection.connect();
// Start the server
var server=app.listen(3035, function () {
console.log("Express server listening on port %d",3035);
});
app.io=io=sio.listen(server);
.
.
.
on the client side:
var socket = io.connect('http://xxxxxx.com:3035');
Your sio is a function. You need to call it with your server as an argument.
Add
server=require('http').Server(app)
sio=require('socket.io')(server)
io.connect() takes a URL such as http://example.com:3035.
You are passing xxxxxx.com:3035 which is not a proper URL form.
Also, note that if you're just trying to connect to the same server and port as the web page came from, you can just use:
io()
or
io.connect()
And the socket.io library will connect back to the same host and port as the web page.
You should initialize default connection on client side by
io.connect( "/" );
According to documentation by default socket.io will connect to the same host and port where rendered webpage is. The / in the path states to connect to default namespace.

Openshift and external mysql db with node-mysql ( Error: connect ECONNREFUSED )

I'm using node js app with external mysql database. It work properly from localhost and other hosting but on openshift I've got this error Error: connect ECONNREFUSED.
var mysql = require('mysql');
var dbConfig = {
host : 'external.mysql.com',
port : '3306',
user : 'user',
password : 'pass',
database : 'test'
};
var connection = mysql.createConnection(dbConfig);
connection.connect();
connection.query('SELECT * FROM test ', function(err, rows, fields) {
if (err) throw err;
});
connection.end();
I'm also tried telnet external.mysql.com 3306 from openshift and it tell
telnet: connect to address 12.34.56.78: Connection refused
Maybe there is some additional params for external connection in openshift
You should make port forwarding to connect remotely services on openshift platform.
Here is the official tutorial and video.

Categories

Resources