To connect create-react-app reactJs with mysql - javascript

I have problem in connecting my create-react-app with mysql using npm.
Its working in nodejs when i tried seperately without npm. But, When i trying to connect using npm install mysql, It throws me an error...
TypeError: http.IncomingMessage is undefined
./node_modules/express/lib/request.js
Here is my code:
var express = require('express');
var mysql = require('mysql');
var connection =mysql.createConnection({
host: 'localhost',
user: 'user',
password: '',
database: 'reactproject'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});

This will not work from react-app, since react code will run from client side.
You should use my-sql connection in different app and run in different port and call as api connection.
Thanks.

Adding from an #ashik answer is correct, but if you still want to use MySQL and reactjs simultaneously, you can start by creating your own boilerplate, there you can use MySQL on the server side and create APi/web service without having to separate the APi server and client projects reactjs.

Related

How can i solve this error when i use mysql module?

I use the node module to connect my local database, then log this error message:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
next is my configuration:
const mysql = require("mysql");
const db = mysql.createPool({
host: "127.0.0.1",
user: "root",
password: "root",
database: "my_db_01"
});
db.query('select 1', (err, results)=>{
if (err) return console.log(err.message);
console.log(results);
});
I can make sure the configuration is correct.
This looks like a frequent issue in the bug reports for the mysql module.
That module hasn't had a commit since 2020. It has 172 open issues. I would consider it abandoned.
Use a different database module. mysql2 seems to be popular.

How to connect to remote phpMyAdmin server using NodeJS + ExpressJS

I'm setting up a GraphQL server however I need to connect to a remote phpMyAdmin server which my friend provided and i am getting the error:
Error: getaddrinfo ENOTFOUND 54.193.13.37/phpmyadmin 54.193.13.37/phpmyadmin:3306
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
here is the code i use to connect
const mysql = require('mysql');
const db = mysql.createConnection({
host: "54.193.13.37/phpmyadmin",
user: "some_user",
password: "some_password",
database:"some_DB",
})
db.connect((err) => {
if (err) throw err;
console.log("Connected!");
});
I've tried removing "http://", adding port, also the IP address 54.193.13.37 is an IP address for his clan website. So, the only way i have access to the database is by 54.193.13.37/phpmyadmin and i dont have any access to any config files or whatsoever, it's just a remote database that he gave me.
Thanks to whoever can help!
just remove the phpmyadmin in the host. Try host: host: "54.193.13.37".
Phpmyadmin is a web interface, but you want to connect directly to the mysql server wich is normaly on Port 3306.

Installing Mongodb to existing Node app causes application to stop working

I have built a node application that uses socket.io and express.
A lot of development has passed with this app but I finally decided to add MongoDB to this app. When I install mongo with npm, everything with socket.io stops working completely. No errors are shown on the client or server side. My cpu usage on chrome spikes up to a crazy amount.
It is a really odd issue because mongo does connect to my server successfully and the app works perfectly without mongo installed into the dependencies.
My server side node application has this structure:
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var url = 'mongodb://hidingmyipaddress:27017';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
io.on('connection', function(socket) {
socket.on('name', function(rm, qqquota){

Nodejs Desktop app with electron starting server when app is launched

Am building my app with electron and nodejs together with express server.
What am trying to achieve is create a desktop app with angular2 handling the routing together with express
Now on my local developenment i have to start the server (express) so that i can connect to the database, so i have the following that connects to the database
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'your-password',
database : 'cloudprint'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
THe above is run on a server.js which i start on the commandline through
npm server.js
Now am abit confused on how i can reuse the above in my app, since when a person installs the app he will not require to have to start server.js from commandline,
How should i approach this during production time.

Node.js: accessing mysql database

I am writing a Node.js application. In my node-modules/abc/static/example.js file, I want to access the mysql records by querying to the database. Is it possible to do so?
When I include mysql like this:
var mysql = require('mysql');
I get an error:
Uncaught ReferenceError: require is not defined
Is it because it is on client side? What are the other options to access mysql database from the 'example.js' file?
I'm going to guess that you're attempting to use server side js in the client side.
You need to npm install a mysql adapter, #coreyg suggested this one (https://www.npmjs.com/package/mysql).
Set up a nodejs project (if you have not already done so):
npm init
npm install --save mysql
You would then typically create some sort of webapp that would serve up a url in the client side. You'd then (using ajax) request the url.
Behind the scenes your webapp then would query the mysql database (using the adaptor you installed) with whatever query you had in mind.
Once your ajax call had finished you'd run your callback which would do what you wanted with the data that had been returned from the database.
Thats the basic theory. If you want further information on how to do this I'd suggest reading this question: How do I get started with Node.js
Following on Neil Munro's Answer,
after initiating nodejs and installing mysql npm package,
you can actually execute your code from the terminal. to do that,
you can create a new js file on the same folder you are executing those commands,
say mysqlTestProject.js
inside this file, you can put what you had previously, (require stuff and all). for example:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
`` codes were taken from https://www.npmjs.com/package/mysql
and then from the terminal (still on the same folder as you were),
type in this line
node ./mysqlTestProject.js

Categories