1

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?

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
jkbadsberg
  • 21
  • 4
  • 2
    Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – E_net4 Jun 22 '17 at 13:31
  • Correct, found that in my `index.js`, I should call `router.get('/', function (req, res)`. I.e. No need to call `next()` in the `router.get()` – jkbadsberg Jun 26 '17 at 12:24

1 Answers1

0

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;
jkbadsberg
  • 21
  • 4