Sunday, 23 April 2017

Connect hostinger DB with node.js

I have a MySQL database setup on Hostinger.com. I can access the database easily with PHP but when I try to access the same with a node.js code it is throwing me error.

My node.js code:

app.get('/hostinger', function(request, response) {
    response.send("Hello");
    var connection = mySequel.createConnection({
        host: "mysql.hostinger.in",
        user: "u**********j",
        password: "*****",
        database: "u**********j"
    });
    connection.connect();
    var buery = "select * from testing";
    connection.query(buery, function(error, result, fields) {
        if (error) {
            console.log(error);
        } else {
            response.send(result);
            console.log(result);
        }
    })
    connection.end();
    // console.log("This is received via a get request in hostinger");
});

The error that I am getting is :

"117.193.192.51" dyno=web.1 connect=0ms service=2ms status=304 bytes=149 protocol=https
2017-04-23T17:59:49.759522+00:00 app[web.1]: { Error: getaddrinfo ENOTFOUND mysql.hostinger.in mysql.hostinger.in:3306
2017-04-23T17:59:49.759532+00:00 app[web.1]:     at errnoException (dns.js:28:10)
2017-04-23T17:59:49.759533+00:00 app[web.1]:     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
2017-04-23T17:59:49.759534+00:00 app[web.1]:     --------------------
2017-04-23T17:59:49.759537+00:00 app[web.1]:     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:141:48)
2017-04-23T17:59:49.759538+00:00 app[web.1]:     at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:52:41)
2017-04-23T17:59:49.759538+00:00 app[web.1]:     at Connection.connect (/app/node_modules/mysql/lib/Connection.js:130:18)
2017-04-23T17:59:49.759539+00:00 app[web.1]:     at /app/index.js:29:16
2017-04-23T17:59:49.759540+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-04-23T17:59:49.759540+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:137:13)
2017-04-23T17:59:49.759541+00:00 app[web.1]:     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2017-04-23T17:59:49.759542+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-04-23T17:59:49.759542+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:281:22
2017-04-23T17:59:49.759543+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2017-04-23T17:59:49.759544+00:00 app[web.1]:   code: 'ENOTFOUND',
2017-04-23T17:59:49.759544+00:00 app[web.1]:   errno: 'ENOTFOUND',
2017-04-23T17:59:49.759545+00:00 app[web.1]:   syscall: 'getaddrinfo',
2017-04-23T17:59:49.759545+00:00 app[web.1]:   hostname: 'mysql.hostinger.in',
2017-04-23T17:59:49.759546+00:00 app[web.1]:   host: 'mysql.hostinger.in',
2017-04-23T17:59:49.759547+00:00 app[web.1]:   port: 3306,
2017-04-23T17:59:49.759548+00:00 app[web.1]:   fatal: true }

Judging from the error log I can see that the host cannot be resolved properly, when I give the same credentials in the PHP file and execute it I am able to run it without any errors.

What could be the possible problem with my code? Also I read in 00webhost that node.js cannot be used to access the DB. No such thing is mentioned in the Hostinger website. If it is so then are there any PaaS service that provides MySQL database that can be readily connected with node.js?



via Nirmal Raj

No comments:

Post a Comment