How to make a request in a localhost mysql database - mysql

I am using mysql and request. I want to make a request in my localhost server. I just need to know how to make the get request into the localhost server because I don't understand how, I'm running into a continuous loop.
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'virtual_currency_price'
});
Request:
router.get('/getprice', function(req, res){
request(connection, function (error, response, body) {
if (!error && response.statusCode == 200){
}
});
});
I'm trying to figure out how to do this but I'm failing very miserably
The table I'm trying to GET from is called "price" and I will be using the datetime to fetch certain prices.

After you establish a connection with your server you can use it to fetch data from database. You don't need to make a request using the module, just use the connection directly to execute queries as below:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'virtual_currency_price'
});
connection.connect();
connection.query('SELECT * from price', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
});
connection.end();
It's recommended that you create a pool of connections instead and reuse them in your app instead of creating it every time you need to access the DB.
See the docs from github.

Related

How to fetch data from MySQL database with Node

I'm new to react, developing a recipe-app and I got a problem while displaying the data from MySQL database. The connection was created successfully, however, I'm not sure about how to reach the data. When I run node server.js in my terminal, I get "connected", When I visit the localhost:8080/users, I get "This site can't be reached" message and in my terminal:
`events.js:187
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Handshake after already enqueuing a Handshake.`
I'm a little stuck here. Anyone knows a solution or direct me a little bit? Thank you so much!
Server.js
const express = require('express');
const app = express();
const PORT = 8080;
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'recipe_app'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
//creating route for the app
app.get('/users', (req, res) => {
connection.connect();
connection.query('SELECT * from users', function(err, rows, fields) {
if (!err) {
res.send(JSON.stringify(rows));
} else {
console.log('Error while performing Query.');
}
});
connection.end();
});
//making server listen to request
app.listen(PORT, () => {
console.log(`Server running at : http://localhost:${PORT}/`);
});
You're trying to reconnect to mysql after the connection has been established.
See my comments on the code below
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'recipe_app'
});
connection.connect((err) => { // This creates the connection
if (err) throw err;
console.log('Connected!');
});
And when you're trying to resolve your GET routes, you're trying to connect again
//creating route for the app
app.get('/users', (req, res) => {
connection.connect(); // reconnect here
Since you're using the default connection method, trying to connect to an already established connection will cause the driver to throw a Handshake error.
If you want to re-use the connection, store it in a variable and then re-use it in other part of your code.
If you want to manage multiple connections instead, I suggest you to look at createPool instead.
Try removing the connection.connect() and connection.end() from app.get

How to configure custom database Mysql in Auth0 Connection?

I'm using auth0 as my authentification services into my project. I really love it, but I have a problem when using custom database(MySql), I sure that I have configured the db.connection parameter to my remote shared hosting database in Plesk. It always show : "[Error] Script execution did not complete within 20 seconds. Are you calling the callback function?", When I trying to run "Create" script.
here the script :
function create (user, callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.23.16',
user : 'user',
password : 'pass',
port : '3306',
database : 'dbname' });
connection.connect();
var query = "INSERT INTO users SET ?";
var insert = {
password: bcrypt.hashSync(user.password, 10),
email: user.email
};
connection.query(query, insert, function (err, results) {
if (err) return callback(err);
if (results.length === 0) return callback();
callback(null);
});
}
What should I'm doing right now to solve this problem? I'm new to this
Thanks..
Regards,
fxbayuanggara
You're trying to connect to a local IP address (192.168.23.16), which will always fail since database scripts and rules are executed from Auth0's servers. You'll need to make your MySQL server accessible from Auth0's IP addresses, which at the time of writing are the following:
US domains: 138.91.154.99, 54.221.228.15, 54.183.64.135, 54.67.77.38, 54.67.15.170, 54.183.204.205, 54.173.21.107, 54.85.173.28
EU domains: 52.28.56.226, 52.28.45.240, 52.16.224.164, 52.16.193.66

Using the phonegap framework with node.js, javascript, html, mysql and css

Can someone please show me an example of how we use phonegap and node.js to push and pull data to mySQL database from an html page/form .
THANKS
EDIT
var connection = mysql.createConnection({ host : 'localhost', user : 'root', database :'nodejsmysql', password : '' });
connection.connect();
connection.query('SELECT * from nodejs', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].fname); }
);
connection.end();
I have the above code which works very well, But I dont want to display my results on the console, instead I want to display them on an html textBox
You need to use custom web service to communicate with your server. You cannot directly manipulate the data on client side. The data manipulation happen only at server side. So, either you will use REST or SOAP, it's up to you how you want to implement it. I suggest you doing more research about the web service.
REST - http://en.wikipedia.org/wiki/Representational_state_transfer
SOAP - http://en.wikipedia.org/wiki/SOAP
More
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
http://blog.revivalx.com/2014/02/25/crud-operation-using-jquery-mobile-on-android-part-2/
var express = require('express');
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodejsmysql'
});
app.use(express.bodyParser());
app.post('/index', function(req, res) {
var date = req.body.date;
connection.query('INSERT INTO reservations(Date) VALUES (? );' , [date], function(err, docs) {
if (err) res.json(err);
res.redirect('users');
});
});
I managed to nail it using this!

Node.js gives error "Can't find variable require"

I'm trying to connect to my local mysql database by using node.js on my website. I have installed node.js on my laptop. This is my code that is executed:
function registerUser()
{
var mysql = require("mysql");
var connection = mysql.createConnection( {
host: 'localhost',
user: 'root',
password: '',
database: 'Fountaint_News_Database',
port: 3306
});
connection.connect();
var query = connection.query('INSERT INTO registered_users VALUES ("test", 22)',
function(err, result, fields) {
if (err) throw err;
console.log('result: ', result);
});
connection.end();
console.log('Connection closed');
}
I haven't done anything else, I'm not sure if there is anything else to setup, but when I runt I just get this error "ReferenceError: Can't find variable: require".
I hope that someone can help me.
Are you running this code on the server side, not on the client don't you? I mean saving this file on a .js file, running via console via
node yourfile.js
after obviously a
npm install mysql
in the same directory?
Obviously you need to bind the registerUser() function to some event :)

Node.js changes in mysql

i have a question regarding about node.js for mysql.
I'm trying to check for changes in the database with node.js and every time it does a change it would do something.
I have the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'socialpodd',
});
connection.connect();
connection.query('SELECT * FROM personal', function(err, rows, fields) {
if (err) throw err;
console.log('Rows: ', rows[0].email);
});
connection.end();
But I want this code to be called once there is a change in the database. I know I could do a poll method, but it doesn't seem that effective and real-time. What would be a good real-time module that would allow me to do it, or is polling my only option?