Client Javascript getting data from backend express server


I have built an Express Server like this:

var mysql = require('mysql2');
var express = require('express');

var app = express();
var PORT = 3000;

app.get('/getDataFromDatabase', function(req, res) {

  var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "", // Password is filled ()
    database: "casesdb"

  con.connect(function(err) {
    if (err) throw err;
    con.query("SELECT * FROM cases", function (err, result, fields) {
      if (err) throw err;

app.listen(PORT, () =>
  console.log(`Example app listening on port ${PORT}!`),

My Goal is to call the /getDataFromDatabase in client javascript and then use that data. How would I go about that?


Try the following on client-side:


    .then(response => response.json())
    .then(data => console.log(data))
    .catch(err => console.error(err));


Answered By – Saif Ali Khan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published