Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a *slightly* prettier node implementation... #24

Merged
merged 3 commits into from
Apr 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 103 additions & 93 deletions nodejs/hello.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
var cluster = require('cluster')
, numCPUs = require('os').cpus().length
, http = require('http')
, numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.pid + ' died');
});

return;
}

var http = require('http')
, url = require('url')
, async = require('async')
, mongoose = require('mongoose')
Expand Down Expand Up @@ -37,107 +51,103 @@ var WorldSchema = new Schema({
}, { collection : 'world' });
var MWorld = conn.model('World', WorldSchema);

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.pid + ' died');

function getRandomNumber() {
return Math.floor(Math.random() * 10000) + 1;
}

function mongooseQuery(callback) {
MWorld.findOne({ id: getRandomNumber()}).exec(function (err, world) {
callback(err, world);
});
} else {
http.createServer(function (req, res) {

// JSON response object
var hello = {message: "Hello, world"};

var path = url.parse(req.url).pathname;
if (path === '/json') {
// JSON Response Test
res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
// Write JSON object to response
res.end(JSON.stringify(hello));
} else if (path === '/mongoose') {
// Database Test
var queries = 1,
worlds = [],
queryFunctions = [],
values = url.parse(req.url, true);

if (values.query.queries) {
queries = values.query.queries;
}
}

function sequelizeQuery(callback) {
World.find(getRandomNumber()).success(function (world) {
callback(null, world);
});
}

res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
http.createServer(function (req, res) {
// JSON response object
var hello = {message: "Hello, world"};

for (var i = 1; i <= queries; i++ ) {
queryFunctions.push(function(callback) {
MWorld.findOne({ id: (Math.floor(Math.random() * 10000) + 1 )}).exec(function (err, world) {
worlds.push(world);
callback(null, 'success');
});
});
}
var path = url.parse(req.url).pathname;

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(worlds));
});
} else if (path === '/sequelize') {
var queries = 1,
worlds = [],
queryFunctions = [],
values = url.parse(req.url, true);

if ( values.query.queries ) {
queries = values.query.queries;
}
switch (path) {
case '/json':
// JSON Response Test
res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
// Write JSON object to response
res.end(JSON.stringify(hello));
break;

case '/mongoose':
// Database Test
var values = url.parse(req.url, true);
var queries = values.query.queries || 1;
var queryFunctions = new Array(queries);

for (var i = 0; i < queries; i += 1) {
queryFunctions[i] = mongooseQuery;
}

res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(results));
});
break;

case '/sequelize':
var values = url.parse(req.url, true);
var queries = values.query.queries || 1;
var queryFunctions = new Array(queries);

res.writeHead(200, {'Content-Type': 'application/json'});
for (var i = 0; i < queries; i += 1) {
queryFunctions[i] = sequelizeQuery;
}

res.writeHead(200, {'Content-Type': 'application/json'});

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(results));
});
break;

case '/mysql':
res.writeHead(200, {'Content-Type': 'application/json'});

pool.getConnection(function(err, connection) {
if (err || !connection) {
return res.end('MYSQL CONNECTION ERROR.');
}

for (var i = 1; i <= queries; i++ ) {
queryFunctions.push(function(callback) {
World.find(Math.floor(Math.random()*10000) + 1).success(function(world) {
worlds.push(world);
callback(null, 'success');
});
function mysqlQuery(callback) {
connection.query("SELECT * FROM World WHERE id = " + getRandomNumber(), function(err, rows) {
callback(null, rows[0]);
});
}

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(worlds));
});
} else if (path === '/mysql') {
var queries = 1,
worlds = [],
queryFunctions = [],
values = url.parse(req.url, true);

if ( values.query.queries ) {
queries = values.query.queries;
var values = url.parse(req.url, true);
var queries = values.query.queries || 1;
var queryFunctions = new Array(queries);

for (var i = 0; i < queries; i += 1) {
queryFunctions[i] = mysqlQuery;
}

res.writeHead(200, {'Content-Type': 'application/json'});

pool.getConnection(function(err, connection) {
for (var i = 1; i <= queries; i++ ) {
queryFunctions.push(function(callback) {
connection.query("SELECT * FROM World WHERE id = " + (Math.floor(Math.random()*10000) + 1), function(err, rows) {
worlds.push(rows[0]);
callback(null, 'success');
});
});
}

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(worlds));
connection.end();
});
async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(results));
connection.end();
});
} else {
// File not found handler
res.writeHead(404, {'Content-Type': 'text/html; charset=UTF-8'});
res.end("NOT IMPLEMENTED");
}
}).listen(8080);
}
});
break;

default:
// File not found handler
res.writeHead(404, {'Content-Type': 'text/html; charset=UTF-8'});
res.end("NOT IMPLEMENTED");
}
}).listen(8080);
3 changes: 2 additions & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
, "async": "0.2.5"
, "mysql": "2.0.0-alpha7"
}
}
, "main": "hello.js"
}