Routes with parameters gets called twice?
Asked Answered
W

2

11

I am creating a NodeJS web application via ExpressJS. I have the following two routes (among others):

app.get('/user/reset/verify', function(req, res) {
    console.log("Executing verification index.");
    res.render("verify/index");
});

app.get('/user/reset/verify/:email/:token', function(req, res) {
    console.log("Executing verification change.");
    res.render("verify/change");
});

When I go to the verification index page, I see "Executing verification index." printed once on the console. However, when I go to the verification change page, I see "Executing verification change." printed twice on the console.

I have noticed that this is a trend with the routes in my app. Routes that contain parameters are always executed twice, while routes without parameters are only (properly) executed once.

Why are the routes with parameters being executed twice?

The views that are being rendered only contain simple HTML - nothing that would cause another request to the page. Also, I am issuing these requests from a Chrome browser.

Platform/Versions:

  • NodeJS: 0.5.5 windows build (running on Win 7)
  • Express: 2.4.6
  • Connect: 1.7.1
Wolfe answered 20/9, 2011 at 1:43 Comment(6)
I'd expect its a node 0.5.5 bug. Try using 0.4.12Son
@Raynos: Alright, I'll try to find a windows executable of 0.4.12 and see if that fixes the issue.Wolfe
there is no windows executable for 0.4.12. Express is not supported with 0.5.x (use at own risk). I recommend you use 0.4 on linuxSon
Do you have any extensions enabled? I had this exact issue with a "performance improvement" extension that would issue a XHR request after page load.Attemper
@Ricardo: Hmm, I don't think I have any extensions enabled. How do I check for enabled extensions? (Do you mean within Chrome or NodeJS)Wolfe
In Chrome, go to Window -> Extensions.Attemper
P
17

The second request is the /favicon.ico Try to console log your request.url in your http_server request handler, you'll see the first is the browser url and the next the favicon.

Pyrrho answered 15/10, 2011 at 2:44 Comment(1)
In my case, the two requests had different headers. So I used if(req.header('Accept') === 'image/webp,image/apng,image/*,*/*;q=0.8') { res.end(); } to not act on the second (image) requestVoroshilovsk
G
-1

If you are using chrome: When you write your url chrome send a get request to check the url before you hit enter.

Try to log the middleware url console.log(req.url) position your console aside your broswer then start to write the url, you will see console logging a get access.

Gapeworm answered 13/4, 2017 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.