ExpressJS app.locals.title gives: Can't set headers after they are sent

210 Views Asked by At

I am new to ExpressJS, so I wanted to set a simple string value, I could translate.

So I tried out globalize-express and set the app title like this:

app.use(function (req, res, next) {
    console.log("App: " + req.Globalize.formatMessage('strings/title'));
    res.locals.title = req.Globalize.formatMessage('strings/title');
    next();
});

followed by:

app.use('/', index);

It looks like it is rendering correctly, but the console is posting the error:

Can't set headers after they are sent.

How can I avoid this error?

1

There are 1 best solutions below

0
On BEST ANSWER

I found that the error was in the related routes/index.js, there was scaffolded another next() call. I updated that file to:

    var express = require('express');
    var app = require("../app.js");
    var router = express.Router();

    router.get('/', function (req, res) {
        res.render('index', {
        });
    });

    module.exports = router;