问题
I have some code written in Jade, with a link in it. The destination of the link is generated by Jade. When the link is clicked, I notice from my console that the GET-request is being executed twice.
Why is this? How can I fix this?
Here is my code:
Jade file:
ul.media-list
each paper in paperList
div.panel.panel-default
div.panel-body
li.media
div.media-left.media-middle
a(href='/publication/view/#{paper.id}')
| Some image
div.media-body
div.btn-group(role='group')
//!!! When this link is being clicked, GET is executed twice !!!
a.btn.btn-default(href='/publication/view/#{paper.id}')
| View
Console:
GET /publication/view/123 200 490ms - 5623
GET /publication/view/123 304 458ms - -
app.js:
var publication = require('./routes/publication');
app.use('/publication', publication);
publication.js:
var express = require('express');
var router = express.Router();
router.get('/view/:id', function (req, res) {
var data;
//Some database functions here
//Just an example
res.render('publication', {someData: data});
});
回答1:
It's just a hypothesis, but it looks like your browser is using some kind of prediction algorithm to pre-load your links even before you click them. Google Chrome do such things.
Express.js answered the second response with 304 Not Modified
response, which indicates that it was sent with valid ETag value to validate previously cached response.
Try to disable network actions prediction in your browser. Here is how it looks in Google Chrome settings (Settings
-> Show advanced settings...
):
Or you may try to verify that the first request is being sent before you're actually clicking on a link by hovering your mouse over it, but not clicking it.
来源:https://stackoverflow.com/questions/29049587/express-why-is-this-get-request-executed-twice