How to import Express js into React js?

advertisements

Is it possible to import Express js into React js? I've used Express js for database connection with MySql which retrieves rows from a table. I need to display result in react app. How can that be achieved?

var express = require('express');
var app = express();

var PORT = 3000;
app.listen(PORT, function(){
  console.log('http://localhost:'+ PORT);
});

var mysql      = require('mysql');
var connection = mysql.createConnection({
   host     : 'localhost',
   user     : 'root',
   password : '',
   database : 'example'
 });

 connection.query('SELECT * from customers', function(err, rows, fields) {
   if (!err)

     console.log('The solution is: ', rows);
   else
     console.log('Error while performing Query.');
 });

 connection.end();


Something like this in node

app.get('/customers', function (req, res) {
 connection.query('SELECT * from customers', function(err, rows, fields) {
  if (!err)
    //process your db result here and then response
   res.json(result);
    connection.end();
 else
   res.status(500).json(err);
   connection.end();
 });

})

in react you can use fetch module

 fetch(`http://<host>:<port>/customers`)
        .then(result=> {
            //here inside result you have the response from your express app
        });