Question: app.use ("*") seems to be calling function multiple times

Question

app.use ("*") seems to be calling function multiple times

Answers 1
Added at 2016-12-26 09:12
Tags
Question

app.use("*", topUsers) is called multiple times..

topUsers is being called multiple times

function topUsers(req, res, next){
    console.log("req.url", req.url)
    findMostUsefullReviewsAggregate(6)
    .then(function(aggregationResult){
        // console.log("aggregationResult", aggregationResult)
        // console.log("***aggregationResult::",  aggregationResult)
        return populateMostUseful(aggregationResult)
    })
    .then(function(populated){
        console.log("<<<<<<<<<<<<<<<<<<TOPUSER CALLED >>>>>>>>>>>>")

        // console.log("POPULATED: ", populated);
        console.log(">>>populateMostUseful.length");

            populated = populated.map(function(e){
                e.momented = moment(e.createdAt.getTime()).fromNow();
                return e;
            })

            req.session.mostUsefulReviews = populated;

        // res.end()
    })
    .catch(function(err){
        console.log("HEADERERR", err);
    });

    next();
}

( some info for later: main.ejs is for ("/"))

when I change it to app.get("/", topUsers) and go to "/"it is only called once (which is what I want).

the console.log for req.url shows "/" for all 3

In my main.ejs I include a header.ejs. I thought that might be a problem. Maybe the request to the header for the include was a problem but I don't think so.

Quick question : If I do app.get("/") would that work on all subroutes like /users/idofuser? I think If I do that the function doesn't get called.

I think app.get("*") also gives me the same problem with the multiple calls.

Edit: when I put next in the .then() it still gets called multiple times.

my real goal it to have something happen on all routes. I already have routes set up. I don't want to go to each route and do something like app.get("/", onemiddleware, topUsers, ). I don't want to put topUSers on each one physically. Do I have to.

Answers to

app.use (&quot;*&quot;) seems to be calling function multiple times

nr: #1 dodano: 2016-12-26 09:12

If you are doing this from a browser loading a web page, the browser is probably requesting multiple resources from the website.

For starters, it often requests the favicon and if there are any other resources in the web page (other scripts, stylesheets, images, etc...) each of those will be a separate request to your web page. You can see exactly what URL is being requested by logging req.url for each request. From a single page load, there should be no reason you would get three requests for /, but you very well could get a request for / follow by other requests for some other resource (and consequently some other URL) from the same server.

Show us the web page you are loading and we can better point out what's going on.


If what you're really trying to do is to share a middleware on a set of routes, then you can create a router, put the middleware on the router with router.use(), define all the routes that you want to have that middleware on that specific router and then hook the router into your app. This will execute the middleware only for routes that match that router and you only have to specify the middleware once for the router and all routes defined on that router will use that middleware. Other routes defined directly on the app object or on other routers will not use the middleware. You will have to define the router as some unique path root or wildcard that helps express know which routes should be passed into your router. We'd have to see the whole universe of paths you plan to use that both do and don't use that middleware for us to know more specifically what to suggest.

Source Show
◀ Wstecz