Last active
August 29, 2015 13:56
-
-
Save mpenick/8811537 to your computer and use it in GitHub Desktop.
Helenus/Express Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test", | |
"description": "An application to test helenus", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "3.x", | |
"helenus": "0.6.x" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helenus = require('helenus'), | |
express = require('express'), | |
util = require('util'), | |
pool = new helenus.ConnectionPool({ | |
//hosts : [], // removed on purpose | |
keyspace : 'nodejs', | |
user : 'nodejs', | |
password : 'nodejs', | |
timeout : 3000, | |
hostPoolSize : 2, | |
}); | |
var cl = helenus.ConsistencyLevel.ONE; | |
pool.on('error', function(err){ | |
console.error(err.name, err.message); | |
}); | |
pool.connect(function(err, keyspace) { | |
if(err) { | |
console.error(err); | |
} else { | |
console.log("connected"); | |
} | |
}); | |
var app = express(); | |
function buildResponse(res, body, status) { | |
res.status(status); | |
res.setHeader('Content-Type', 'text/plain'); | |
res.setHeader('Content-Length', Buffer.byteLength(body)); | |
res.end(body); | |
} | |
function printCQLErrors(err) { | |
console.log("CQL Error: " + err); | |
} | |
app.use(function(req, res, next){ | |
var data = ""; | |
req.on('data', function(chunk){ data += chunk}) | |
req.on('end', function(){ | |
req.rawBody = data; | |
next(); | |
}) | |
}) | |
app.get('/:key', function(req, res) { | |
var query = "SELECT value FROM nodejs.test WHERE key = ?"; | |
pool.cql(query, [req.param('key')], { consistencyLevel: cl }, function(err, results) { | |
if(err) { | |
printCQLErrors(err); | |
buildResponse(res, "[cql error]", 500); | |
} else { | |
if(results.length > 0) { | |
buildResponse(res, results[0].get('value').value, 200); | |
} else { | |
buildResponse(res, "[no value]", 404); | |
} | |
} | |
}); | |
}); | |
app.post('/:key', function(req, res) { | |
var query = "INSERT INTO nodejs.test (key, value) VALUES (?, ?)"; | |
pool.cql(query, [req.param('key'), req.rawBody], { consistencyLevel: cl }, function(err, results) { | |
if(err) { | |
printCQLErrors(err); | |
buildResponse(res, "[cql error]", 500); | |
} else { | |
buildResponse(res, "[success]", 200); | |
} | |
}); | |
}); | |
app.listen(8000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE KEYSPACE nodejs WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}; | |
CREATE TABLE nodejs.test ( | |
key text PRIMARY KEY, | |
value text | |
); | |
CREATE USER nodejs WITH PASSWORD 'nodejs'; | |
GRANT ALL PERMISSIONS ON nodejs.test TO nodejs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment