Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2011 18:38
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Node + MySQL + JSON
process.env.TZ = "UTC";
var util = require("util"),
express = require("express"),
gzip = require("connect-gzip"),
mysql = require("mysql");
var client = mysql.createClient({
host: /* mysql host, e.g., "" */,
port: /* mysql port, e.g., 3306 */,
user: /* mysql user, e.g., "username" */,
password: /* mysql password, e.g., "password" */,
database: /* mysql database, e.g., "database" */
var server = express.createServer();
server.use(express.static(__dirname + "/public"));
server.get("/things/:id([0-9]+).json", function(request, response, next) {
+ ","
+ " t.favorite_color,"
+ " t.airspeed_velocity"
+ " FROM things t"
+ " WHERE = ?", [], function(error, rows) {
if (error) return next(error);
server.listen(/* http port, e.g., 8888 */, /* http host, e.g., "" */);
Copy link

bstaats commented Dec 16, 2011

Im assuming if one wanted to d3.nest the query results you could simply put d3 package in the same dir and use require('d3')??

Copy link

I'd probably use GROUP BY and do that on the SQL side. But conceivably you could use D3 to do that as well. I'm not sure if D3 is packaged up correctly to work as require("d3"), but in theory that could be made to work.

Copy link

bstaats commented Dec 16, 2011

Yea.. I would normally do it SQL side, but in this particular circumstance performance favors transformations in the middleware and not in DB. Ill give it a go. Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment