Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB: Get server-time with an http request in CouchApp

I need the server-time for "user-is-online" stats in my CouchApp. I work with jquery.couch.js and would prefer to have a url, e.g. /db/_design/app/time - which gets me a timestamp. How do I realize this?

like image 761
jukempff Avatar asked May 26 '11 08:05

jukempff


1 Answers

A show function could do that:

function(doc, req) {
    // _design/myapp/_show/now

    // First version possibly incompatible with some spidermonkey versions.
    //var now = new Date();
    var now = new Date().getTime();
    var output = JSON.parse(JSON.stringify(now)) + "\n";

    return { code: 200
           , headers: { "Content-Type": "text/plain"
                      }
           , body:output
           };
}

The server also includes a Date header that you might want to use.

$ curl -D- http://localhost:5984
HTTP/1.1 200 OK
Server: CouchDB/1.1.0 (Erlang OTP/R14B)
Date: Fri, 27 May 2011 00:28:31 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 40
Cache-Control: must-revalidate

{"couchdb":"Welcome","version":"1.1.0"}
like image 122
JasonSmith Avatar answered Oct 24 '22 01:10

JasonSmith