Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

locals.pretty in Express 4

I want to send pretty html using Express 4. How can I user app.locals.pretty with Express 4? Old syntan doesn't work:

app.locals.pretty = true;

Whole block of code:

app.set('port', process.env.PORT || 1339);
app.set('views', __dirname + '/app/views');
app.set('view engine', 'jade');
app.set('view options', { layout: false });

app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(morgan());
app.use(bodyParser());
app.use(methodOverride());
app.use(cookieParser('123'));

app.use(session({
    secret: '123',
    maxAge: new Date(Date.now() + 3600000),
    store: SessionStore
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
    app.locals.pretty = true;
}
like image 524
user3215609 Avatar asked Apr 25 '14 08:04

user3215609


1 Answers

For pretty printing json, you can set the number indented spaces which should turn on pretty printing:

app.set('json spaces', 2);

Then just do:

res.json({ foo: 'bar', baz: 1234567890 });

Similarly you can app.set('json replacer', function(..){...}); to set the replacer parameter to JSON.stringify that is used under the hood.

UPDATE: for pretty printing html, I don't think there is anything built into Express for that..

like image 189
mscdex Avatar answered Oct 22 '22 11:10

mscdex