0

I have an admin panel where I customize things like my site's name, tagline, &c. The below code does exactly what I want it to do, but only for the index page:

app.get("/", function (req, res) {
  app.service("customization").find().then(function (result) {
    res.render("index", Object.assign({ layout: "layouts/default" }, result[0]));
  });
});

How do I make this apply to all routes (so, the entire site)? I've tried placing * in place of /, but that hasn't helped.

EDIT: Replacing / with * and placing this bit of code before all my routes made every page render my index view, which is not ideal. For the time being, I will just duplicate this code for each route, changing the route and view options. I have code like this:

app.get("/admin/:page", require("connect-ensure-login").ensureLoggedIn("/admin"), function (req, res) {
  app.service("customization").find().then(function (result) {
    res.render("admin/" + req.params.page, Object.assign({ user: req.user, title: "Admin", layout: "layouts/admin" }, result[0]));
  });
});

To ensure I'm not duplication too much, but still...

NetOperator Wibby
  • 1,354
  • 5
  • 22
  • 44

1 Answers1

1

You can use * as a wildcard to apply a router to all the URLs in place of the asterisk.

However, if you need to reuse an asynchronous result in the rest of your routers, the following is a good approach. Perform the asynchronous call before your routers and use the result in the rest of the routers.

app.use(function(req, res, next) {
    app.service("customization").find().then(function (result) {
        app.set('result', result);
        next();
    });
});

This will set an application variable that can be accessed from anywhere server side by using app.get.

Note the requirement of next() inside of the asynchronous callback. The application will wait for the result before moving on, ensuring you don't move on the next routers without it.

app.get("/", function (req, res) {
    res.render("index", Object.assign({ layout: "layouts/default" }, app.get('result')[0]));
});

In your routers you can use app.get to access the result variable as needed.

roflmyeggo
  • 1,724
  • 16
  • 18
  • Because I don't want to render the `index` page for every route. I'm rendering different pages for every route. Also, your code didn't work in my project, unfortunately. – NetOperator Wibby Apr 11 '16 at 04:28
  • You stated that you want your code to apply to all routes. The code you posted renders HTML and once you render one page you cannot render another. Please state your desired end goal in the original post and I can try to help. – roflmyeggo Apr 11 '16 at 05:01
  • Obviously the code posted is wrong...also, I'm pretty sure I stated my goal. Typically, one has to show that they've tried to achieve a solution when they ask questions on SO, hence me sharing my code. That does not work. Hence my query. – NetOperator Wibby Apr 11 '16 at 05:05
  • Using asterisk works on ExpressJS 4.x as a wildcard which I have updated my answer with. Perhaps check the order of your routers. – roflmyeggo Apr 11 '16 at 05:41
  • Do you just want to make the `app.service` call before all of your routers? If so, I have update my answer above. – roflmyeggo Apr 11 '16 at 14:47
  • Sweet, it works! Thanks a lot, I'm still learning how to use middleware. I see `app.set` was what I needed. – NetOperator Wibby Apr 11 '16 at 15:00