Search code examples
javascriptnode.jspretty-printkoakoa-router

Pretty print HTML output with Koa?


Is there a way to have your HTML output that comes from a Jade template be beautified? Something similar to Express's app.locals.pretty = true; ? I'm using koa-router and koa-views if that is relevant.

server.js

const koa = require('koa');
const views = require('koa-views');
const serve = require('koa-static');
const router = require('./routes');

const app = koa();

app.use(serve(`${__dirname}/public`));
app.use(views(`${__dirname}/views`, { extension: 'jade' }));
app.use(router.routes());

app.listen(3000, () => {
  console.log('Server listening at http://localhost:3000');
});

routes/index.js

const router = require('koa-router')();

router.get('/', function *() {
  yield this.render('index');
});

module.exports = router;

views/index.jade

html
  head
    title Hello
    meta(charset='utf-8')
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
    link(type='text/css', rel='stylesheet', href='css/style.css')

  body
    h1 Hi

Solution

  • you could try:

    app.use(views(`${__dirname}/views`, { extension: 'jade', pretty:true}));
    

    that should do the trick... on the other hand, i use koa-jade instead of koa-views. My current code looks like this:

    var Jade  = require('koa-jade');
    
    var jade = new Jade({
        viewPath: path.resolve(__dirname,"jade"),
        debug: true,
        pretty: true,
        compileDebug: true,
        basedir: path.resolve(__dirname,"jade"),
        app:app
      })
    

    in production mode i just set pretty to false...

    HTH