Home » Node.js

Show data from MYSQL table by using Node.js

In this article, we are going to learn how to show records from MYSQL table using Node.js server?
Submitted by Manu Jemini, on November 23, 2017


In this article, our task is to show all the data of table clients so what we are going to do is to prepare a server file of node.js where we require the MySQL module to work with MySQL and after that, we prepare a connection by using mysql.createConnection() method of MySQL module.

There are certain things that we have to define inside our create connection methods like host, user, password, and database name.

As that we are all set to go with our connection, all we need to create a SQL query to select data basically a select query and after that we can use query() method to execute query statement with a callback function in which we can throw error if any and print the data we get in JSON format.

Database details:

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database: demo
  • Table name: clients

Server File

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "",
  user: "root",
  password: "123",
  database: "demo"

con.connect(function(err) {
  if (err) throw err;
  var sql = "select * from clients";

  con.query(sql, function (err, result) {
    if (err) throw err;

Discussing steps that we have followed in our server file:

  • Require MySQL module.
  • Create Connection variable using mysql.createConnection() method.
  • Connect by using con.connect() method.
  • Creating SQL select query.
  • Execute query by using .query() method.
  • Show data on the console.
Node.js - how to show/display data in Node.js

If you have any query do comment.

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.