Permalink
Browse files

count hits

  • Loading branch information...
1 parent 7b6e9e3 commit afd028efceb8ae2ed9af0f083f3437990d88f31f Michael Sofaer committed Aug 15, 2012
Showing with 13 additions and 5 deletions.
  1. +1 −1 db.js
  2. +0 −1 public/javascripts/sample_app.js
  3. +12 −3 web.js
View
2 db.js
@@ -5,7 +5,7 @@ function create_db(){
pg.connect(process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael", function(err, client) {
client.query("CREATE TABLE account(account_id BIGINT PRIMARY KEY, cdate TIMESTAMPTZ default now());");
client.query("CREATE TABLE domain(domain_id BIGINT PRIMARY KEY, account_id BIGINT REFERENCES account(account_id), cdate TIMESTAMPTZ default now());");
- client.query("CREATE TABLE hit(hit_id BIGINT PRIMARY KEY, domain_id BIGINT REFERENCES domain(domain_id), cdate TIMESTAMPTZ default now());");
+ client.query("CREATE TABLE hit(hit_id serial PRIMARY KEY, domain_id BIGINT REFERENCES domain(domain_id), cdate TIMESTAMPTZ default now());");
})
}
@@ -2,7 +2,6 @@ CloudFlare.define("sample_app",
[ "cloudflare/dom", "sample_app/config", "cloudflare/console"],
function(dom, config, console)
{
- console.log("stuff");
return {
name: "Sample App",
dom: dom
View
15 web.js
@@ -72,10 +72,14 @@ function log_hit(domain_id, respond){
query.on('end', function() {
var query2 = client.query('select count(*) from hit where domain_id = $1', [domain_id]);
- query2.on('end', function() {
- console.log(query2);
+ query2.on('row', function(row) {
+ respond(row.count);
+ });
+ query2.on('error', function(something) {
+ console.log(something);
respond("0");
});
+
});
query.on('error', function(something) {
@@ -143,7 +147,12 @@ app.post('/hit', function(request, response) {
count : count
}));
}
- log_hit(request.body.domain_id, respond);
+
+ if (!request.body.domain_id) {
+ respond({error: "no domain_id"}, 400)
+ } else {
+ log_hit(request.body.domain_id, respond);
+ }
});

0 comments on commit afd028e

Please sign in to comment.