Day 50, we lost one member

Many things happened today.

The new class started and we, became, the step children. One of the classmates said, it feels like that there is a new baby in the house. haha

Code review with Steven. Learned something very useful. Apparently, inside Express, I can use

res.json(output);
//instead of
res.write(JSON.stringify(output));
//not 100% sure...

Finally, our class lost one member, Sonomi, and I was shocked. I noticed that she wasn’t around but I thought she might be sick at home. Then 10 minutes before I called it a day, I was told that she was gone from the class. What I don’t like is that: there is no announcement nor no explanation on what happened. Sigh…

Also spent a couple of hours breaking down our 600 line API file into multiple files. Refactor code is always an interesting experience. I am glad that it turned out to be ok. After refactoring, I spent most of my time working on several API endpoints. It was fun.

Standard

The surefire way to connect to mysql in node.js with node-mysql

It is amazing that how much trouble I had trying to establish connection to local mysql server. A quick search on StackOverflow shows that I am not alone!

I think I found a surefire way to establish local mysql connections – use socket instead of TCP/IP. Briefly, there are two ways to establish mysql connections, either with TCP/IP (you need host and port number) or socket (you only need the filepath of the socket).

In theory, there shouldn’t be much difference between them. However, finding a file is definitely easier than finding the ip and the port number.

Here is the code to connect to a Mysql db from MAMP.


var mysql = require('mysql');

var connection = mysql.createConnection({
 user : 'root',
 password : "root",
socketPath : '/Applications/MAMP/tmp/mysql/mysql.sock',

//for non MAMP, might be

//socketPath : '/tmp/mysql.sock',
});

connection.connect(function(err) {
 if (err) {
 console.log('db_connection_err', err);
 return;
 }
});

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
 if (err) throw err;
console.log('The solution is: ', rows[0].solution);

});

connection.end();

Standard

Node.js debugging

– Don’t use port 8080

– Run: node -debug-brk app.js

– Start node-inspector

– Visit the URL it outputs to you

– Good parts: can use ‘debugger / breakpoints’

– Bad parts: NO jasmine-node

– Bonus: heard that webstorm has a good Node debugging tool. Should check it out.

Standard