Q:

How does node connect to database in node.js?

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now