5

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?

jukempff
  • 965
  • 6
  • 12

1 Answers1

5

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"}
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • your show function returns '[object, object]' but I changed it to var now = new Date().getTime(); and it worked! Thanks alot! – jukempff May 27 '11 at 09:23
  • Altough this is very old question can not help but comment. I used show function above and it is wotking. But result is CACHED. Meaning you get same result over and over again. So be careful. – bamanow May 29 '16 at 18:30