How can I get Express to output nicely formatted HTML?

前端 未结 9 604
名媛妹妹
名媛妹妹 2020-11-28 01:13

When using Express for Node.js, I noticed that it outputs the HTML code without any newline characters or tabs. Though it may be more efficient to download, it\'s not very r

相关标签:
9条回答
  • 2020-11-28 01:51

    There is a "pretty" option in Jade itself:

    var jade = require("jade");
    
    var jade_string = [
        "!!! 5",
        "html",
        "    body",
        "        #foo  I am a foo div!"
    ].join("\n");
    
    var fn = jade.compile(jade_string, { pretty: true });
    console.log( fn() );
    

    ...gets you this:

    <!DOCTYPE html>
    <html>
      <body>
        <div id="foo">I am a foo div!
        </div>
      </body>
    </html>
    

    I doesn't seem to be very sophisticated but for what I'm after -- the ability to actually debug the HTML my views produce -- it's just fine.

    0 讨论(0)
  • 2020-11-28 01:52

    In express 4.x, add this to your app.js:

    app.locals.pretty = app.get('env') === 'development';
    
    0 讨论(0)
  • 2020-11-28 01:53

    Do you really need nicely formatted html? Even if you try to output something that looks nice in one editor, it can look weird in another. Granted, I don't know what you need the html for, but I'd try using the chrome development tools or firebug for Firefox. Those tools give you a good view of the DOM instead of the html.

    If you really-really need nicely formatted html then try using EJS instead of jade. That would mean you'd have to format the html yourself though.

    0 讨论(0)
提交回复
热议问题