Calling app.use in nodejs to add file middleware in bulk

45 Views Asked by At

I'm trying to add data-driven code to load the middleware for various load-and-go files, that is, commonly used ones ones that HTML sent via sendFile() has in its BODY section. I am asking for help figuring out how to substitute the bold parts (which turned into stars) in the code below when providing a callback. Anybody have a better way?

var endpoints = [
    {
        "url":      'views/jquery-ui-1.12.1.custom/jquery-ui.css',
        "type":     'text/css'
    },
    {
        "url":      'views/app.css',
        "type":     'text/css'
    }
    {
        "url":      'views/favicoon.ico',
        "type":     'image/x-icon'
    }
];

// Function that the router will use.
function route_handler (req,res) {
    session=req.session;
    if(session.userid){
        res.sendFile('**views/home.html**', {root:__dirname});
    }
    else res.sendFile('**views/index.html**', {root:__dirname})
}

// And generate the middleware.
for ( var route of endpoints ) {
    app.get(route.url, route_handler);
}

Thanks In Advance.

0

There are 0 best solutions below