1

How to connect MySQL database in CoffeeScript. How to run this.I created MySQL connection using node.js and display record in terminal but how to display this on browser.I am using ECO templating for displaying on browser. I need database connection using coffeescript

file - db_test.js

  var mysql = require("mysql");
  var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "test"
});

  con.connect(function(err){
    if(err){
       console.log('Error connecting to Db');
       return;
    }
    console.log('Connection established');
});

 con.query('SELECT * FROM users',function(err,rows){
      if(err) throw err;
      for (var i = 0; i < rows.length; i++) {
       console.log(rows[i].username);
    };
});

con.end(function(err) {
  // The connection is terminated gracefully
  // Ensures all previously enqueued queries are still
  // before sending a COM_QUIT packet to the MySQL server.
});
O. Jones
  • 103,626
  • 17
  • 118
  • 172
Darshika
  • 31
  • 6

1 Answers1

2

CoffeeScript is just javascript

Any javascript library should be usable in CoffeeScript because at the end of the day, the CS is just getting converted to JS and running in your usual runtime. This means you would connect just the same (same libraries, methods, etc), just different syntax.

The only advantage of CoffeeScript is that it's nicer to write than JS, if you don't know CoffeeScript, there's little reason to use it at all, and in fact you're just making your life harder by adding a compilation step to your production.

If you must absolutely use CS, there are tools to cross-compile between the two (like js2.coffee), but I strongly suggest you learn CoffeeScript and write it on your own so you get its advantages and not just its burdens. There are plenty of resources online.

Kevin Chavez
  • 949
  • 1
  • 11
  • 27
  • Note that it currently looks unlikely that CoffeeScript will ever be updated to support newer JS features (ECMAScript 6+), so you might want to consider using Babel instead: https://babeljs.io/ – Matt Browne Jul 16 '15 at 17:13