Q:

How does node connect to database in node.js?

belongs to collection: Node.js programming Exercises

0

How does node connect to database in node.js

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

To connect to MySQL server, we will create a connection with mysql module. So, first include the mysql module, then call the create connection method. Here is the sample code to connect to database.

var mysql = require("mysql");
var con = mysql.createConnection({
    host: "hostname",
    user: "username",
    password: "password",
    database: "database"
});
con.connect(function(err){
    if(err) {
        console.log('Error connecting to Db');
        return;
    }
    console.log('Connection Established');
})
con.end(function(err) { 
});

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How do you iterate over the given array in node.js... >>
<< Write a node.js program to get files or directorie...