3

We are using the node-postgres (pg on NPM) for our app but were having issues so we decided to go back to the examples on the Wiki:

https://github.com/brianc/node-postgres/wiki/Example

When we run the example, each http request inserts two records into the Postgres ("visit") table. Is this the desired behaviour...?

We published the example code to Heroku: https://node-postgres-example.herokuapp.com
(Note: Visit using Google Chrome)

Note: we made 3 changes to the server.js code from the Wiki to make it run on Heroku this is on GitHub: https://github.com/dwyl/postgres-connection-pool-test

(the changes we made to server.js are purely to (1) create the visit table if it does not already exist, (2) to get the postgres connection string from process.env.DATABSE_URL and (3) to listen on process.env.PORT on Heroku. all the rest of the code is as per the Wiki example)

2
  • When implementing an HTTP service, checking for the number of requests is the very first thing one would think of doing. Commented Mar 13, 2016 at 21:59
  • @vitaly-t agreed. thanks. Commented Mar 14, 2016 at 16:25

2 Answers 2

0

Your client (browser) seems to make two requests. If you use curl from the command line, the example works as advertised and returns a continuous visit counter:

→ curl -i https://node-postgres-example.herokuapp.com/
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
Content-Type: text/plain
Date: Sun, 13 Mar 2016 14:19:42 GMT
Transfer-Encoding: chunked
Via: 1.1 vegur

You are visitor number 40

→ curl -i https://node-postgres-example.herokuapp.com/
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
Content-Type: text/plain
Date: Sun, 13 Mar 2016 14:20:00 GMT
Transfer-Encoding: chunked
Via: 1.1 vegur

You are visitor number 41
Sign up to request clarification or add additional context in comments.

3 Comments

you're right, curl only causes a single insert into the visit count. So this could be a browser issue... But the browser in question is Google Chrome... so we need to get to the bottom of it. Checked Safari & Firefox have the desired behaviour.
I guess you are confusing "requests" with "visits".
It's not my code. I'm just trying to run the example code and was seeing this issue. thanks. you have helped us get to the bottom of the problem.
-1

The second request is almost certainly the browser requesting /favicon.ico which is an anomaly in the web tech stack in that it's a request the browser makes implicitly without an explicit reference in some containing HTML document. If you handle the favicon request separately, perhaps using express-favicon, you will resolve your issue and only log 1 visit per page load.

4 Comments

request != visit. A request is just that, like a browser requesting the favicon. If you want to count visits, you have to get a bit more fancier than just counting requests. (Cookies, etc.)
@okket you are not contributing helpfully here. I understand precisely what an HTTP request is and is not. I'm explaining to the OP what is going on. If you find the posted answers deficient, post a better one and let the community vote on them.
I don't see how it helps the OP if you are calling requests "visits", show him a workaround for a perfectly legal request by the browser and calling it an "anomaly".
The question has several facets. The distinction between the definition of an http request, which is a hard technical event and a logical "visit" which is a business analytics concept is not the only aspect to the question. There is also the fact that different browsers and other HTTP clients behave differently, specifically some automatically issue a (probably unexpected by OP) request for the favicon, and some don't. So I'm telling him what is going on as well as showing him how to adjust his code if he so desires (thus redefining what counts as a visit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.