Javascript 中间件执行多次?

Javascript 中间件执行多次?,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试使用express制作一个url缩短器应用程序。我有两个中间件分别用于路由/shorten/:url*和/:code。不知何故,当我发出像/shorten/iamarshad.com这样的请求时(未格式化的请求,将使validateUrl方法失败),处理该请求的中间件有时会执行两次,有时会执行三次。为什么会这样? route.js的代码: var express = require("express"); var router = express.Router(); var crypt

我正在尝试使用express制作一个url缩短器应用程序。我有两个中间件分别用于路由
/shorten/:url*
/:code
。不知何故,当我发出像
/shorten/iamarshad.com
这样的请求时(未格式化的请求,将使validateUrl方法失败),处理该请求的中间件有时会执行两次,有时会执行三次。为什么会这样? route.js的代码:

var express = require("express");
var router = express.Router();
var crypto = require("./crypto");

var styles = "<style>@import url('https://fonts.googleapis.com/css?family=Cormorant+Garamond');" +
            "body{background: #fefefe; word-wrap: break-word;}" +
            "p {font-size: 30px;color: #b33c66;font-family: 'Cormorant Garamond', monospace;text-align: center;" +
            "margin-top: 40vh;font-weight: 500;word-spacing: 2px;}</style>";

function verifyUrl(req, res, next) {
    console.log("/shorten middleware called");
    req.params.url +=  req.params[0];
    console.log(req.params.url);
    if (validateUrl(req.params.url)) {
        req.db.collection("counter")
        .find({_id: "counter"})
        .toArray(function (err, docs) {
            if (err) console.error("Error occurred while getting COUNTER document:", err);

            req.encodedId = crypto.encode(docs[0].count);

            next();
        });
    }
    else {
        var elem = "<p>Please enter correct and formatted url!</p>";
        res.send(styles + elem);
    }
}


function incrementCounter(req, res, next) {
    // increasing counter
    req.db.collection("counter")
        .update(
            { 
                _id: "counter"
            },
            {
                $inc : {
                    count : 1
                }
            }
        );

    next();
}

function insertUrlDocument(req, res, next) {
    //inserting new url document
    var obj = {original_url: req.params.url, _id: req.encodedId, entry_time: new Date().toUTCString()};
    req.db.collection("urls")
        .insert(obj, function(err, data) {
            if(err) console.error("Error happened while adding new document:", err);
        });
    next();
}

function sendResponse(req, res) {
    var elem = "<p>" + JSON.stringify({'original_url': req.params.url, 'short_url': 'https://shorten-that.herokuapp.com/' + req.encodedId}) + "</p>";
    res.send(styles + elem);
}

function validateUrl(url) {
  var format = /(http:\/\/|https:\/\/)[a-z0-9\-]+[.]\w+/;
  return (format.test(url));
}

router.get("/:code", function(req, res) {
    console.log("/:code middleware called with url", req.params.code);
    var code = req.params.code.toString();
    // searching short-url-id
    req.db.collection("urls")
        .find({_id: code})
        .toArray(function(err, docs) {
            if(err) console.error("Error occurred while searching urls:", err);
            console.log(docs);
            if(docs.length > 0)
                res.redirect(docs[0]["original_url"]);
            else {
                var elem = "<p>Oops, wrong url requested!</p>";
                res.send(styles + elem);
            }
        });
});

// better solution needed 
router.get("/shorten/:url*", [verifyUrl, incrementCounter, insertUrlDocument, sendResponse]);

module.exports = router;

浏览器将请求
favicon.ico
,而您的
/:code
与此匹配,您可以使用favicon中间件捕获favicon。如果你的中间件在你之前,那么它应该在你的中间件之前捕获它。@keith我正在尝试为favicon服务。为什么需要favicon?
为什么需要favicon
,浏览器会要求您在浏览器选项卡顶部看到的图标使用此图标。@keith,好的。我觉得我问了一个愚蠢的问题。在添加favicon之后,一些how
/:id
中间件被调用了两次。您可以查看代码和其他内容。在github,url返回404。浏览器将请求
favicon.ico
,并且您的
/:code
与此匹配,您可以使用favicon中间件捕获favicon。如果你的中间件在你之前,那么它应该在你的中间件之前捕获它。@keith我正在尝试为favicon服务。为什么需要favicon?
为什么需要favicon
,浏览器会要求您在浏览器选项卡顶部看到的图标使用此图标。@keith,好的。我觉得我问了一个愚蠢的问题。在添加favicon之后,一些how
/:id
中间件被调用了两次。您可以查看代码和其他内容。在github上,该url返回404。
var express = require("express")
    , mongo = require("mongodb").MongoClient
    , port = process.env.PORT || 8080
    , path = require("path")
    , routes = require("./routes")
    , favicon = require("serve-favicon");  

var app = express();
app.use(favicon(path.join(__dirname, 'public','favicon.png')));
app.use(express.static(path.join(__dirname, "public")));

var url = 'mongodb://localhost:27017/url-shortener';

mongo.connect(url, function(err, db) {
    if (err) console.error("Error occurred while connecting to db:", err);

    console.log("successfully connected to db.");

    app.use(function(req, res, next) {
        req.db = db;
        next();
    });        

    app.use("/", routes);
});

app.listen(port, function() {
   console.log("App running on", port); 
});